Yii2 在什么条件下不工作?

Yii2 在什么条件下不工作?,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,这里我重写了find()方法 class ActiveRecord extends BaseActiveRecord{ public static function find() { return parent::find() ->where(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id]) -&g

这里我重写了
find()
方法

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]);
    }
}
现在如果我使用这样的条件

\common\models\Order::find()->where(['stastus'=>'3'])->count(); 
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]);
    }
}
活动记录全局条件在条件I之前执行 在订单模型中使用,然后在 活动记录全局条件

如果我像这样使用活动记录条件

\common\models\Order::find()->where(['stastus'=>'3'])->count(); 
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覆盖每个where。

这就是Yii2
ActiveRecord
的工作方式。当您调用方法
where()
时,它会重置条件,即使该方法不是空的,当您调用
andWhere()时也是如此
它为现有的条件添加了新的条件。

您应该将
中的where
以及where
更改为
条件中的
onCondition
,我想它是
\yii\db\ActiveRecord
的别名,作为
父项::find()
如果查看
find()
方法,则返回
ActiveQuery的对象

\yii\db\ActiveRecord

 return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
您可以在这里看到添加一个附加条件

class ActiveRecord extends BaseActiveRecord{
    return parent::find ()
            ->onCondition ( [ 'and' ,
                [ '=' , static::tableName () . '.application_id' , 1 ] ,
                [ '=' , static::tableName () . '.branch_id' , 2 ]
    ] );
}
现在如果你打电话

\common\models\Order::find()->where(['status'=>'3'])->createCommand()->rawSql; 
它将生成以下查询

SELECT * FROM `order` 
   WHERE (`status` = 3) 
   AND 
   ((`order`.`application_id` = 1) AND (`order`.`branch_id` = 2))

这里有些事情很棘手。
\common\models\Order::find()->where(['status'=>'3'])->count()原始sql请我现在在办公室,到家后会回复我无法安静地听到你想说什么@Sajid,我希望解决方案对你有效如果你还有其他问题,你可以创建一个新的问题