Php Yii2:find()中的orderby->;where()方法

Php Yii2:find()中的orderby->;where()方法,php,yii2,Php,Yii2,我正在尝试对数据库进行查询,以按类别检索喜爱产品的列表 在类别中有一个名为sortcode的属性 在此之后,我有一个$categoryId:[3242123163431232] 及产品 $products = Product::find()->where(['category'=>$categoryids])->all(); 但结果并不像我预期的那样,$products中的项按索引排序 现在我想所有产品在类别3242应该排名第一,然后到1231。。。 我如何得到我想要的结果?

我正在尝试对数据库进行查询,以按类别检索喜爱产品的列表

在类别中有一个名为sortcode的属性 在此之后,我有一个$categoryId:[3242123163431232] 及产品

$products = Product::find()->where(['category'=>$categoryids])->all();
但结果并不像我预期的那样,
$products
中的项按索引排序 现在我想所有产品在类别3242应该排名第一,然后到1231。。。 我如何得到我想要的结果? 对不起,我的英语不好! 提前谢谢,祝你今天愉快

参考

提及


尽量在条件允许的地方使用

$products = Product::find()
     ->where(['in','category',$categoryids])
     ->orderBy('category DESC')
     ->all();
或者,如果您想按类别的快捷码对其进行排序,则应加入categorys表,该表尚未测试,但应能正常工作:

$products = Product::find()
    ->where(['in','category',$categoryids])
    ->joinWith(['categorys' => function ($query) {
        $query->orderBy('shortcode');
    }])
    ->all();
不要在产品模型中添加类别关系

public function getCategorys()
{
    return $this->hasOne(Category::className(), ['id' => 'category']);
}

尽量在条件允许的地方使用

$products = Product::find()
     ->where(['in','category',$categoryids])
     ->orderBy('category DESC')
     ->all();
或者,如果您想按类别的快捷码对其进行排序,则应加入categorys表,该表尚未测试,但应能正常工作:

$products = Product::find()
    ->where(['in','category',$categoryids])
    ->joinWith(['categorys' => function ($query) {
        $query->orderBy('shortcode');
    }])
    ->all();
不要在产品模型中添加类别关系

public function getCategorys()
{
    return $this->hasOne(Category::className(), ['id' => 'category']);
}

我不使用orderby,因为category_id具有与sortcode字段不同的顺序我不知道您会得到什么?请提供详细信息。同时添加表信息。我不使用orderby,因为类别id的顺序与sortcode字段不同。我不知道您会得到什么?请提供详细信息。同时添加表格信息。谢谢!你救了我一天!非常感谢。你救了我一天!