首页 文章

Yii2有很多或哪里

提问于
浏览
1

您好我对Yii2有很多问题 .

我的表结构:

-------------------------
----col1------col2-------

------3---------4--------

------4---------1--------

现在我在我的Model类中有方法:

getCols(){
   return $this->hasMany(Cols::className,['col1'=>'id']);
}

现在我想得到所有记录,例如col1 = 4或col2 = 4,那么我该如何设置或声明?我知道我能做到

getCols(){
   return $this->hasMany(Cols::className,['col1'=>'id','col2'=>'id']);
}

但是这个方法返回记录,例如col1 = 4 AND col2 = 4

问候

1 回答

  • 0

    如果我理解正确的话 .

    getCols(){
       return $this->hasMany(Cols::className(), ['col1' => 'id'])
        ->orOnCondition(['col2' => 'id']);  
    }
    

    如果有效,请在评论中确认 .

    REF1:如何在Yii2的ON条件下使用常量有很多关系REF2:http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#orOnCondition()-detail

相关问题