Yii Framework Forum: Active Records IN statement - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Active Records IN statement Rate Topic: -----

#1 User is offline   Tatty 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-December 10

Posted 08 December 2010 - 07:22 AM

Hi,

We are running Yii 1.03. This is the first time I have been experimenting with Active Records - up to now we have just been using custom SQL queries. I feel I am misunderstanding something though...

Take this code:


$sqlCondition = "person_id IN (:personId)";
$sqlParams['personId'] = '1111,2222';
$TransferObject = MTransfer::model()->findAll(array(
					'condition' => $sqlCondition,
					'params' => $sqlParams
					));


I would have thought this would return two objects, one for person_id 1111, and one for person_id 2222. However it only returns one object - in this case 1111.
If I reverse the numbers ('2222,1111') then it will give me the object for person 2222 instead.

Forgive me if I am misunderstanding something obvious. How can I get this query to work?
0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,462
  • Joined: 04-October 10

Posted 08 December 2010 - 09:40 AM

How do you check the amount of records returned? Did you loop through the results -foreach? Did you count the returned value?
0

#3 User is offline   seb 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 29-June 09

Posted 08 December 2010 - 10:39 AM

View PostTatty, on 08 December 2010 - 07:22 AM, said:

Take this code:
$sqlCondition = "person_id IN (:personId)";
$sqlParams['personId'] = '1111,2222';
$TransferObject = MTransfer::model()->findAll(array(
					'condition' => $sqlCondition,
					'params' => $sqlParams
					));



Parameter name in the params should be the same as in condition (including ':'):

$sqlCondition = "person_id IN (:personId)";
$sqlParams[':personId'] = '1111,2222';

Maybe this cause your result.

Also why not to use CDbCriteria features?
    $criteria = new CDbCriteria;
    $criteria->addInCondition('person_id', array('1111','2222'));
    $TransferObject = MTransfer::model()->findAll($criteria);

1

#4 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,462
  • Joined: 04-October 10

Posted 08 December 2010 - 10:43 AM

seb is right... didn't see the two dots for the parameter settting
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users