Yii2 从视图调用find()方法时,自定义ActiveRecord类不工作?

Yii2 从视图调用find()方法时,自定义ActiveRecord类不工作?,yii2,yii2-advanced-app,yii-components,Yii2,Yii2 Advanced App,Yii Components,我正在重写find方法并全局添加where条件,这是我的代码 class Order extends \common\components\ActiveRecord \common\components\ActiveRecord namespace common\components; use Yii; use yii\db\ActiveRecord as BaseActiveRecord; class ActiveRecord extends BaseActiveRecord{

我正在重写find方法并全局添加where条件,这是我的代码

class Order extends \common\components\ActiveRecord    
\common\components\ActiveRecord

namespace common\components;
use Yii;

use yii\db\ActiveRecord as BaseActiveRecord;

class ActiveRecord extends BaseActiveRecord{
    public static function find() {
        return parent::find()
           ->where(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id])
           ->andWhere(['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]);
    }
}
当我像这样从视图调用模型时,该查找不起作用

 echo  \common\models\Order::find()->where(['status'=>'0'])->count();
使用
和where()

class ActiveRecord extends BaseActiveRecord {
    public static function find() {
        return parent::find()
            ->andWhere(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id])
            ->andWhere(['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]);
    }
}
使用
和where()

class ActiveRecord extends BaseActiveRecord {
    public static function find() {
        return parent::find()
            ->andWhere(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id])
            ->andWhere(['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]);
    }
}

尝试使用onCondition()进行更改


尝试使用onCondition()进行更改


因为您在
ActiveRecord
中使用的find函数中使用了
where()
,然后在此语句中使用了where()

echo  \common\models\Order::find()->where(['status'=>'0'])->count();
第二个最终会覆盖第一个

您需要做的是在所有需要使用ActiveRecord中的原始条件的情况下使用andWhere()。试一试:

echo  \common\models\Order::find()->andWhere(['status'=>'0'])->count();
这应该行得通


请再次记住,在需要应用原始条件的所有位置使用andWhere()

,因为您在
ActiveRecord
中使用的查找函数中使用了
where()
,然后在此语句中使用了where()

echo  \common\models\Order::find()->where(['status'=>'0'])->count();
第二个最终会覆盖第一个

您需要做的是在所有需要使用ActiveRecord中的原始条件的情况下使用andWhere()。试一试:

echo  \common\models\Order::find()->andWhere(['status'=>'0'])->count();
这应该行得通


请再次记住,在所有需要应用原始条件的地方使用andWhere()

@Muhammad Omer AslamSir请看一看,这让我很困惑,因为您过度使用了
where()
条件,在查询中将
where(['status'=>0'])
更改为
->和where(['status'=>0])
@Yupik oo是的,这意味着我需要替换所有application@MuhammadOmer AslamSir请看一看,这让我很困惑,因为您过度使用了
where()
条件,在查询中将
where(['status'=>'0'])
更改为
->和where(['status'=>0])
@Yupik oo yes所以这意味着我需要替换所有应用程序中的where如果没有where条件,查询会发生什么情况..?什么都没有。这些条件仍然适用。@in同一头骨@Sajid如果在任何查询中where为false,那么..?如果没有where条件,那么查询会发生什么..?什么都没有。这些条件仍然适用。@in同一头骨@Sajid如果任何查询中的where为false,那么。。?