database relations - how to define a relationship in Yii2 with orOnCondition -
i logging changes in database table called audit_field. given model retrieve audit_fields model of related models.
for example:
<?php class job extends activerecord { public function getauditfields() { $link = []; // put here "1=1" ? return $this->hasmany(auditfield::classname(), $link) ->oroncondition([ 'audit_field.model_id' => $this->job_id, 'audit_field.model_name' => get_class($this), ]) ->oroncondition([ 'audit_field.model_id' => arrayhelper::map($this->getjobteches()->all(), 'id', 'id'), 'audit_field.model_name' => 'app\models\jobtech', ]); } public function getjobteches() { return $this->hasmany(jobtech::classname(), ['job_id' => 'job_id']); } }
what want:
select * audit_field (...my or conditions...); // or select * audit_field (1=1) , (...my or conditions...);
what get:
select * audit_field (1=0) , (...my or conditions...)
found answer, don't use hasmany
, instead return activequery
:
<?php class job extends activerecord { public function getauditfields() { return auditfield::find() ->oroncondition([ 'audit_field.model_id' => $this->job_id, 'audit_field.model_name' => get_class($this), ]) ->oroncondition([ 'audit_field.model_id' => arrayhelper::map($this->getjobteches()->all(), 'id', 'id'), 'audit_field.model_name' => 'app\models\jobtech', ]); } }
Comments
Post a Comment