How to get records from mysql table in cakephp 3.0 with OR condition?
I want to get records from a table for names starting with a, b or c but with status = pending in cakephp 3.0. I am trying to query below. Please check what is wrong with it as it also gets results starting with the name ':'
$this->loadModel('DbArtists');
/*** fetching records alphabetically ***/
$artists_q = $this->DbArtists->find()->where(['album_status' => 'pending'])->orWhere(['name ' => 'A%', 'name ' => 'B%', 'name' => 'C%']);
$artist = $artists_q->all();
echo "<pre>";print_r($artist);die;
+3
source to share
1 answer
Follow these steps: -
$artists_q = $this->DbArtists->find()->where([
'album_status' => 'pending',
'OR' => [['name LIKE' => 'A%'],['name LIKE' => 'B%'],['name LIKE' => 'C%']],
]);
Or you can say this too: -
$query = $articles->find()->where(['album_status' => 'pending'])
->orWhere(['name LIKE' => 'A%'])
->orWhere(['name LIKE' => 'B%'])
->orWhere(['name LIKE' => 'C%']);
Link: - https://book.cakephp.org/3.0/en/orm/query-builder.html
+3
source to share