如何在Yii2的ON条件下使用常数有很多关系
发布时间:2020-12-13 16:39:05 所属栏目:PHP教程 来源:网络整理
导读:我尝试创建一个多态关联,在Rails中是常见的,但不幸的是不是在Yii2中.作为实现的一部分,我需要定义关系: public function getImages(){ return $this-hasMany(RecipeImage::className(),['imageable_id' = 'id','imageable_type' = 'Person']);} 但是这不起
我尝试创建一个多态关联,在Rails中是常见的,但不幸的是不是在Yii2中.作为实现的一部分,我需要定义关系:
public function getImages() { return $this->hasMany(RecipeImage::className(),['imageable_id' => 'id','imageable_type' => 'Person']); } 但是这不起作用,因为“Person”被视为当前模型的属性,但它是一个常量(多态关联的类名称). 如果我尝试使用’andWhere’,它会在WHERE子句中添加条件,而不是ON子句,导致仅返回现有映像的记录. public function getImages() { return $this->hasMany(RecipeImage::className(),['imageable_id' =>" id'])-> andWhere(['imageable_type' => 'Ingredient']); } 如何定义关系?没有andOn方法.
在这种情况下,您可以使用ANDOnCondition方法修改ON条件:
public function getImages() { return $this->hasMany(RecipeImage::className(),['imageable_id' => 'id']) ->andOnCondition(['imageable_type' => 'Person']); } 官方文件: > andOnCondition: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |