2

Hello i have question about Yii2 has many.

My table structure :

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

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

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

and now i have method in my Model class:

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

and now i want to get all records where for example col1 = 4 or col2 = 4, so how can i set or statement ? I know that i can do

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

but this method returns me records for example where col1 = 4 AND col2 = 4

Greetings

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
Juri Bojka
  • 305
  • 2
  • 8
  • 18

1 Answers1

0

If i understand you correctly.

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

Please confirm in the comment if it works.

REF1 : How to use constant in the ON condition in Yii2 hasMany relation

REF2 : http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#orOnCondition()-detail

Community
  • 1
  • 1
Syed Ekram Uddin
  • 2,907
  • 2
  • 29
  • 33