nguyendh
(Duynguyen0511)
July 4, 2011, 3:25pm
1
Hi folks,
It comes to my attention that I can not fully understand how $condition and $params works in findByAttributes. I appreciate if someone has done similar things to share with Yii newcomers.
In most case, this is how I use findByAttributes
Person::model()->findByAttributes(array('first_name'=>$firstName,'last_name'=>$lastName));
Thanks
mikl
(Mike)
July 5, 2011, 6:53am
2
Your example should be sufficient for most cases. If you need to add more complex conditions you have several options:
Supply a $condition as string:
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
'status=1'
);
Supply a $condition as string that contains placeholder and $params as array with placeholder values:
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
'status=:status',
array(':status'=>1)
);
Supply a $condition as a CDbCriteria :
$criteria=New CDbCritieria;
$critieria->condition='status=1';
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
$criteria
);
Supply a $condition as array with property values for CDbCriteria :
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
array(
'condition'=>'status=:status',
'params'=>array(':status'=>1)
)
);
I need to Find last matched model using findByAttributes … Help me
mikl
(Mike)
July 21, 2011, 2:23pm
5
Can you be more clear what you try to do?
jowen
(Ljowen985)
July 22, 2011, 2:40am
6
hi guys,
I put this in the controller site. But how i can show all the result that match the query in View site because my view result always show 1 record even though i gt many record in database that match.
$post = detail::model()->find(‘country=:country’, array(‘country’ => Malaysia));
View site:
<?php echo $post->name; ?><br/>
<?php echo $post->email; ?><br/>
<?php echo $post->gender; ?><br/>
<?php echo $post->country; ?><br/>
<?php echo $post->dateOfBirth; ?><br/>
<?php echo $post->status; ?><br/>
<?php echo $post->religion; ?><br/>
<?php echo $post->hp_Number; ?><br/>
<?php echo $post->address; ?><br/>
Pls help… XD
jowen
(Ljowen985)
July 25, 2011, 1:04am
8
hi Mike,
change find() to findAll() izzit??
mikl
(Mike)
July 25, 2011, 7:03am
9
Yes. And please first read the guide. These are really basics that are explained well there. The forum is meant for questions that you could not get answered from the guide:
http://www.yiiframework.com/doc/guide/1.1/en/database.ar#reading-record
traprajith
(Traprajith)
August 3, 2011, 10:19am
10
Mike, Sorry for the late reply… i got that …using ‘order’=>‘id DESC’
Model::model()->findAllByAttributes(array('id'=>1),array('order'=>'id DESC'));
be carefulll of:
if your class extends from CActiveRecord and implements an interface, then an error occurs when you call an interface method for this result, but this only happen if you obtain your result using findBySql instead of findByAttributes,
zainiafzan
(Zainiafzan)
August 22, 2012, 9:30pm
12
jowen:
hi guys,
I put this in the controller site. But how i can show all the result that match the query in View site because my view result always show 1 record even though i gt many record in database that match.
$post = detail::model()->find(‘country=:country’, array(‘country’ => Malaysia));
View site:
<?php echo $post->name; ?><br/>
<?php echo $post->email; ?><br/>
<?php echo $post->gender; ?><br/>
<?php echo $post->country; ?><br/>
<?php echo $post->dateOfBirth; ?><br/>
<?php echo $post->status; ?><br/>
<?php echo $post->religion; ?><br/>
<?php echo $post->hp_Number; ?><br/>
<?php echo $post->address; ?><br/>
Pls help… XD
Just sharing…
$post = detail::model()->find(‘country=:country’, array(‘country’ => Malaysia));
change to
$post = detail::model()->findAll(‘country=:country’, array(’:country’ => ‘Malaysia’));
How we can add select option in findByAttributes
I want to run below query
SELECT auto_key_activation, allow_epa_in_deceased FROM users WHERE users_id = $users_id
using
$userRecord = Users::model()->findByAttributes(array(‘id’=>$userId));
traprajith
(Traprajith)
September 24, 2013, 12:20pm
14
Your example should be sufficient for most cases. If you need to add more complex conditions you have several options:
Supply a $condition as string:
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
'status=1'
);
Supply a $condition as string that contains placeholder and $params as array with placeholder values:
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
'status=:status',
array(':status'=>1)
);
Supply a $condition as a CDbCriteria :
$criteria=New CDbCritieria;
$critieria->condition='status=1';
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
$criteria
);
Supply a $condition as array with property values for CDbCriteria :
Person::model()->findByAttributes(
array('first_name'=>$firstName,'last_name'=>$lastName),
array(
'condition'=>'status=:status',
'params'=>array(':status'=>1)
)
);
is it we can use ‘!=’, ‘<’, ‘>’ inside the condition?
ray7551
(Ray7551)
April 23, 2014, 4:05pm
15
neovash
(Mark)
July 31, 2014, 2:56am
16
Not sure if this is the right thread for this question, but i will go ahead and try.
using, model()->findAllByAttributes, with CHtml::ListData, can you show me the correct way to display 2 column values for the, display in a dropdownlist, :
i would like the Name and Cardnumber be displayed on the dropdown menu.
$levelList=CHtml::ListData(card::model()->findallByAttributes(
array("id"=>$id))),'id','name'.'(cardNumber)');
AlexInt
(Alex)
July 31, 2014, 8:15pm
17
Neovash:
Not sure if this is the right thread for this question, but i will go ahead and try.
using, model()->findAllByAttributes, with CHtml::ListData, can you show me the correct way to display 2 column values for the, display in a dropdownlist, :
i would like the Name and Cardnumber be displayed on the dropdown menu.
$levelList=CHtml::ListData(card::model()->findallByAttributes(
array("id"=>$id))),'id','name'.'(cardNumber)');
$levelList = CHtml::listData(Card::model()->findAllByAttributes(array('id'=>$id)), 'id', 'name');
I’m not sure about concatenating ‘name’ to ‘cardNumber’.
neovash
(Mark)
August 1, 2014, 2:59am
18
AlexInt:
$levelList = CHtml::listData(Card::model()->findAllByAttributes(array('id'=>$id)), 'id', 'name');
I’m not sure about concatenating ‘name’ to ‘cardNumber’.
found a solution for my concern:
$criteria = new CDbCriteria;
$criteria->select = 't.guest_cc_id, CONCAT(t.card_holder_name,"(",t.cc_no,")") as cardNameNo'; // select fields which you want in output
$criteria->condition = 't.guest_id = '.$guest_id;
$data = GuestCc::model()->findAll($criteria);
$levelList=CHtml::ListData($data,'guest_cc_id','cardNameNo');
I Need Count of retrieved records, any solution
This is my Records array
$user = Mobilizer::model()->findByAttributes(array(‘email’=>$_POST[‘Mail’][‘id_mobilizer_receiver’]));
How can i get count from them.?
Bizley
(Bizley)
April 2, 2015, 7:33am
20
$userCount = Mobilizer::model()->countByAttributes(array('email'=>$_POST['Mail']['id_mobilizer_receiver']));