重写find方法
新建一个query
PHP Code复制内容到剪贴板
- namespace common\models\query;
- use backend\modules\invest\models\ProjectRecord;
- use yii\db\ActiveQuery;
- use yii\helpers\ArrayHelper;
- class ProjectQuery extends ActiveQuery
- {
- /**
- * 仅当前项目的
- * @return $this
- */
- public function onlyCurrentProject()
- {
- $tableName = $this->modelClass;
- $projectId = \Yii::$app->tool->getCurrentPid();
- $ids = ProjectRecord::find()->select("entity_id,attribute")->where(['project_id'=>$projectId,'entity'=>$tableName])->all();
- if(!$ids){
- return $this->andWhere('0=1');
- }
- $attribute = $ids?$ids[0]->attribute:"id";
- $ids = ArrayHelper::getColumn($ids,"entity_id");
- return $this->andWhere(['in', $attribute, $ids]);
- }
- }
在每个需要用到这个where条件的model,加一下:
PHP Code复制内容到剪贴板
- use common\models\query\ProjectQuery;
- public static function find()
- {
- return Yii::createObject(ProjectQuery::className(), [get_called_class()]);
- }
在所有的find方法加一个后缀,onlyCurrentProject:
如:
PHP Code复制内容到剪贴板
- Job::find()->onlyCurrentProject()->one();