Simple help required with Active Records

I need clarification in regard with these two queries.

Assume the following SQL query:

SELECT * from emp where name="Mark" and job="Manager";

//$post=Post::model()->find($condition,$params);

//$post=Post::model()->findByAttributes($attributes,$condition,$params);

Would the first corresponding statement be the following?

  1. $emp=Emp::model()->find(‘name=:name AND job=:job’,array(’:name’=>‘Mark’,’:job’=>‘Manager’));

And how would I write the second statement? Any other alternatives? I don’t have a local site running on the machine I’m using now to test it myself.

Thanks for the help.

I would suggest to use CDbCriteria if you have more than one parameter.

So is it this then?

$criteria=new CDbCriteria;

$criteria->condition=‘name=:name AND job=:job’;

$criteria->params=array(’:name’=>‘Mark’,’:job’=>‘Manager’);

$emp=Emp::model()->find($criteria);

But is it any different from using

$emp=Emp::model()->find(‘name=:name AND job=:job’,array(’:name’=>‘Mark’,’:job’=>‘Manager’)); ?

I just want to know if the above statement will work. Because the queries I’m going to use aren’t more complex than just two comparisons. Thanks.

  1. will work.

  2. $post=Post::model()->findByAttributes(array(‘name’=>‘Mark’, ‘job’=>‘Manager’));

Yes, it will work that way too. I suggested just to use the other form because it gets pretty ugly soon if you want to add a limit, order, … But no, your solution will work totally fine.

Oh thanks a lot. :)

And one more doubt. About update.

$emp=Emp::model()->findByAttributes(array(‘name’=>‘Bob’));

$emp->job=‘The Builder’;

$emp->save();

Does this mean that Bob’s name will be updated to the ‘The Builder’, or will it add a fresh record with all the details of Bob but with name ‘The Builder’ ?

Why don’t you want to read the guide or API? :) Here is the link: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail

Bob’s job will be changed to ‘The Builder’.

Forums are better. ;)

And thanks. That solves what I wanted to know.

Actually I did go through them. Right now I’m unable to test it. So the forum’s the compiler. :)

Thanks, Andy. And Tropi. :)