quiktester
(Arjun Quikast)
December 22, 2011, 5:24pm
1
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?
$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.
Tropi
(Daniel Lang)
December 22, 2011, 5:35pm
2
I would suggest to use CDbCriteria if you have more than one parameter.
quiktester
(Arjun Quikast)
December 22, 2011, 5:44pm
3
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.
Tropi
(Daniel Lang)
December 22, 2011, 6:15pm
5
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.
quiktester
(Arjun Quikast)
December 22, 2011, 6:17pm
6
andy_s:
will work.
$post=Post::model()->findByAttributes(array(‘name’=>‘Mark’, ‘job’=>‘Manager’));
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’ ?
andy_s
(Arekandrei)
December 22, 2011, 6:25pm
7
quiktester:
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’.
quiktester
(Arjun Quikast)
December 22, 2011, 6:33pm
8
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.