findByAttributes example

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



Your example should be sufficient for most cases. If you need to add more complex conditions you have several options:

  1. Supply a $condition as string:





  1. Supply a $condition as string that contains placeholder and $params as array with placeholder values:






  1. Supply a $condition as a CDbCriteria:

$criteria=New CDbCritieria;






  1. Supply a $condition as array with property values for CDbCriteria:








Thanks for inputs, Mike.

I need to Find last matched model using findByAttributes … Help me

Can you be more clear what you try to do?

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

find() !== findAll()

hi Mike,

change find() to findAll() izzit??

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:

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,

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


$userRecord = Users::model()->findByAttributes(array(‘id’=>$userId));

is it we can use ‘!=’, ‘<’, ‘>’ inside the condition?

The best way to find examples is to find the test code:

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');

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);


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.?

$userCount = Mobilizer::model()->countByAttributes(array('email'=>$_POST['Mail']['id_mobilizer_receiver']));