Tab
Tab

Reputation: 309

How to use IN/AND clause in Yii framework?

I have a MySQL table with 3 columns: ID, name, user. I wish to use the following SQL with Yii framework:

$sql = "SELECT * FROM my_table WHERE idName=".$name." AND user IN .$arrayOfUsers;
// $arrayOfUsers is an array of int [0]->1, etc.

I tried in three different ways, but without success:

1)

$sql = "SELECT * FROM my_table WHERE idName=".$name." AND user IN .$arrayOfUsers;
$command = $connection->createCommand($sql);            
$dataReader = $command->query();          
$query = $dataReader->readAll();

The error is:

PHP Error [8]

Array to string conversion

2)

$query = Yii::app()->db->createCommand()
    ->select('*')
    ->from('my_table')
    ->where(array('and', array('in', 'user', $arrayOfUsers), array('idName' => $name)))
    ->queryAll();

The error is:

PHP Error [8]

Undefined offset: 0

3)

 $query = Yii::app()->db->createCommand()
        ->select('*')
        ->from('my_table')
        ->where(array('and', array('in', 'user', $arrayOfUsers), 'idName='.$name)))
        ->queryAll();

The error is:

CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'idName=7)' at line 3. The SQL statement executed was: SELECT * FROM my_table WHERE (user IN ('1', '2', '3', '4')) AND (idName=7)

Upvotes: 3

Views: 9256

Answers (2)

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

There has many ways to do but I'd like to use Active Record to handle this thing. However, your question was around Query Builder, I give you correct one

Edited: (As your comment, idName is big INT instead of var char)

1) You got error because you pass $arrayOfUsers which was array, not expected string on your sql. It should be

 $connection=Yii::app()->db;
        $sql = "SELECT * FROM my_table WHERE idName=".$name." AND (user IN(".implode(',',$arrayOfUsers)."))";
        $command = $connection->createCommand($sql);
        $query = $command->queryAll();

2) Using Query builder and where operator

$query = Yii::app()->db->createCommand()
            ->select('*')
            ->from('my_table')
            ->where(array('in', 'user', $arrayOfUsers))
            ->andwhere('name = :name', array('idName'=>$name))
            ->queryAll();

3) If you want to wrap them together, it'll be fine, but they look unsightly like this

$query = Yii::app()->db->createCommand()
            ->select('*')
            ->from('my_table')
            ->where(array('and', 'idName= ' . $name, array('in', 'user', $arrayOfUsers)))
            ->queryAll();

More references how to use where operator from official document

// WHERE id=1 or id=2
where('id=1 or id=2')
// WHERE id=:id1 or id=:id2
where('id=:id1 or id=:id2', array(':id1'=>1, ':id2'=>2))
// WHERE id=1 OR id=2
where(array('or', 'id=1', 'id=2'))
// WHERE id=1 AND (type=2 OR type=3)
where(array('and', 'id=1', array('or', 'type=2', 'type=3')))
// WHERE `id` IN (1, 2)
where(array('in', 'id', array(1, 2))
// WHERE `id` NOT IN (1, 2)
where(array('not in', 'id', array(1,2)))
// WHERE `name` LIKE '%Qiang%'
where(array('like', 'name', '%Qiang%'))
// WHERE `name` LIKE '%Qiang' AND `name` LIKE '%Xue'
where(array('like', 'name', array('%Qiang', '%Xue')))
// WHERE `name` LIKE '%Qiang' OR `name` LIKE '%Xue'
where(array('or like', 'name', array('%Qiang', '%Xue')))
// WHERE `name` NOT LIKE '%Qiang%'
where(array('not like', 'name', '%Qiang%'))
// WHERE `name` NOT LIKE '%Qiang%' OR `name` NOT LIKE '%Xue%'
where(array('or not like', 'name', array('%Qiang%', '%Xue%')))

http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder

Upvotes: 12

Ninad
Ninad

Reputation: 1881

Try this

$criteria = new CDbCriteria();
$criteria->select = "*";
$criteria->condition = "idname = :name ";
$criteria->params = array ( 
':name' => $name,
);
$criteria->addInCondition('user', $arrayOfUsers);
my_table::model()->findAll($criteria);

Upvotes: 4

Related Questions