Postgresql 与postgres数组数据类型相关的Yii2覆盖条件

Postgresql 与postgres数组数据类型相关的Yii2覆盖条件,postgresql,yii2,Postgresql,Yii2,我试图在Yii2中创建一个与postgres数组列的关系,但它给了我一个错误(毫不奇怪) 仅仅设置标准onCondition()似乎不起作用 有人有使用postgres数组类型和Yii2关系的经验吗?如果我可以这样做来覆盖默认操作符,并在条件下支持数组列类型,那就太好了 /** * @return \yii\db\ActiveQuery */ public function getMyRelation() { return $this->hasMany(ModelName::c

我试图在Yii2中创建一个与postgres数组列的关系,但它给了我一个错误(毫不奇怪)

仅仅设置标准onCondition()似乎不起作用

有人有使用postgres数组类型和Yii2关系的经验吗?如果我可以这样做来覆盖默认操作符,并在条件下支持数组列类型,那就太好了

/**
 * @return \yii\db\ActiveQuery
 */
public function getMyRelation()
{
    return $this->hasMany(ModelName::className(), ['@>', 'id', '{'.intval($this->rel_id).'}'])->alias('myRelation');
}

您不能以这种方式创建
hasMany
关系-
ActiveRecord
不支持这种语法,并且您不能使用模型属性来定义关系,因为此方法可能在实际模型初始化之前执行(例如,如果您正在构建联接)

您可以创建getter,该getter将使用
ActiveQuery
获取相关模型-它不是真正的关系,您将无法使用它进行即时加载或联接,但对于单个模型的延迟加载应该可以正常工作:

public function getMyRelation() {
    $query = ModelName::find()
        ->andWhere(['@>', 'id', '{'. (int) $this->rel_id .'}'])
        ->alias('myRelation');
    $query->primaryModel = $this;
    $query->multiple = true;
    return $query;
}
public function getMyRelation() {
    $query = ModelName::find()
        ->andWhere(['@>', 'id', '{'. (int) $this->rel_id .'}'])
        ->alias('myRelation');
    $query->primaryModel = $this;
    $query->multiple = true;
    return $query;
}