php – Yii2 ActiveQuery在链接数组中使用OR
发布时间:2020-12-13 22:53:43 所属栏目:PHP教程 来源:网络整理
导读:我想在ActiceRecord扩展的类中的hasMany函数中的$link数组中使用OR运算符. 例如,我想获得与用户帐户相关的交易.在sql中它会像SELECT * FROM transactions WHERE fromAccountId =:id ORAccountId =:id但是我怎么能用Yii2写这个 public function getTransact
我想在ActiceRecord扩展的类中的hasMany函数中的$link数组中使用OR运算符.
例如,我想获得与用户帐户相关的交易.在sql中它会像SELECT * FROM transactions WHERE fromAccountId =:id ORAccountId =:id但是我怎么能用Yii2写这个 public function getTransactions() { return $this->hasMany(Transaction::className(),[ 'fromAccountId' => 'id','toAccountId' => 'id' ]); } 解决方法
链接ActiveQuery使用数组的键作为名称列,值 – 作为列的值.
因为代码不起作用(where(fromAccountId,toAccountId)IN(‘id’,’id’)): [ 'fromAccountId' => 'id','toAccountId' => 'id' ] 你可以在getTransactions()中重写hasM??any行为 public function getTransactions() { $query = Transaction::find(); $query->multiple = true; return $query->where(['OR',['fromAccountId' => $this->id],['toAccountId' => $this->id] ]); } 它按预期支持本机行为: $model->getTransactions() // return as yiidbActiveQuery $model->transactions // return as array of Transactions models 但不适用于$model-> find() – > with(‘transactions’),因为需要设置$query->链接.而是需要使用连接…. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |