open-ecommerce.org
open-ecommerce.org

Reputation: 1814

yii2 findAll not working passing in parameter from variable

I am trying to get the following query:

SELECT * FROM `book_category_tree` WHERE `id` IN (8, 9, 10, 15)

According the following yii2 documentation:

http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#findAll%28%29-detail

If i use:

$rows = BookCategoryTree::findAll([8,9,10,15]);

It works perfect but when ever I try to use a variable with the values yii2 doesn't recognize the values:

$myValues = '8,9,10,15';
$rows = BookCategoryTree::findAll([$myValues]);

and generates the following query:

SELECT * FROM `book_category_tree` WHERE `id`='8,9,10,15'

I am getting crazy to avoid this I also tried:

$rows = BookCategoryTree::find()->where(['in','id',[$myValues]])->all();

But I have no luck.

Any ideas welcome,

Thanks

Upvotes: 1

Views: 762

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

you should use where condition with operator notation like this

 $rows = BookCategoryTree::find()->where( ['in', 'id', [8,9,10,15]])->all();

or

 $myValues = [8,9,10,15];
 $rows = BookCategoryTree::find()->where(['in','id',$myValues])->all();

if you have value in string you can use explode for build an array

 $myValues =explode(',', $string_values);

Upvotes: 2

Related Questions