[英]Yii2 - Calling model functions on entire activeRecords?
What I am trying to do 我要做什么
I want to query a specific set of records using active model like so 我想使用像这样的活动模型查询一组特定的记录
$jobModel = Jobs::find()->select('JOB_CODE')->distinct()->where(['DEPT_ID'=>$dept_id])->all();
Then I want to assign a flag attribute to the records in this activerecord based on whether they appear in a relationship table 然后,我想根据它们是否出现在关系表中为该activerecord中的记录分配一个标志属性
What I have tried 我尝试过的
So in my job model, I have declared a new attribute inAccount
. 因此,在我的工作模型中,我在inAccount
声明了一个新属性。 Then I added this function in the job model that sets the inAccount
flag to -1 or 0 based on whether a record is found in the relationship table with the specified account_id
然后,我在作业模型中添加了此函数,该模型根据是否在关系表中找到具有指定account_id
的记录,将inAccount
标志设置为-1或0
public function assignInAccount($account_id){
if(JobCodeAccounts::find()->where(['JOB_CODE'=>$this->JOB_CODE])->andWhere(['ACCOUNT_ID'=>$account_id])->one() == null){
$this->inAccount=0;
}
else{
$this->inAccount = -1;
}
}
What I have been doing is assigning each value individually using foreach like so 我一直在做的是像这样单独使用foreach分配每个值
foreach($jobModel as $job){
$job->assignInAccount($account_id);
}
However, this is obviously very slow because if I have a large number of records in $jobModel
, and each one makes a db query in assignInAccount()
this could obviously take some time if the db is slow. 但是,这显然很慢,因为如果我在$jobModel
有大量记录,并且每个记录都在assignInAccount()
进行数据库查询, assignInAccount()
如果数据库很慢,这显然会花费一些时间。
What I am looking for 我在寻找什么
I am wondering if there is a more efficient way to do this, so that I can assign inAccount
to all job records at once. 我想知道是否有更有效的方法来执行此操作,以便我可以一次将inAccount
分配给所有作业记录。 I considered using afterFind()
but I don't think this would work as I need to specify a specific parameter. 我考虑过使用afterFind()
但是我认为这不起作用,因为我需要指定一个特定的参数。 I am wondering if there is a way I can pass in an entire model (or at least array of models/model-attributes and then do all the assignations running only a single query. 我想知道是否有一种方法可以传递整个模型(或至少传递模型/模型属性数组,然后执行仅在单个查询中进行的所有分配)。
I should mention that I do need to end up with the original $jobModel
activerecord as well 我应该提一下,我确实需要以原始的$jobModel
activerecord结尾
Thanks to scaisEdge's answer I was able to come up with an alternative solution, first finding the array of jobs that need to be flagged like so: 感谢scaisEdge的回答,我得以提出一个替代解决方案,首先找到需要标记的作业阵列,如下所示:
$inAccountJobs = array_column(Yii::$app->db->createCommand('Select * from job_code_accounts where ACCOUNT_ID = :account_id')
->bindValues([':account_id' => $account_id])->queryAll(), 'JOB_CODE');
and then checking each job record to see if it appears in this array like so 然后检查每个作业记录以查看它是否出现在此数组中,如下所示
foreach($jobModel as $job){
if(in_array($job->JOB_CODE, $inAccountJobs))
$job->inAccount = -1;
else
$job->inAccount = 0;
}
Does seem to be noticeably faster as it requires only a single query. 似乎确实要快得多,因为它只需要单个查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.