findAllByAttributes

Hi,

If I have a table like this …

id | parent_id

1 | 0

2 | 1

When I do this




Company::model()->findAllByAttributes(array("id"=>1, "parent_id" =>1))



It returns no records which is correct because its looking for a record with ID = 1 AND PARENT_ID = 1

Well I want it to return both records with ID = 1 OR PARENT_ID = 1

Is this possible, and preferably without writting the SQL manually.

maybe you should try condition here… or just use find() from the beginning

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAllByAttributes-detail

Thanks.

I do not think find() will make a difference. Its the condition thats important I believe, can anyway give me an example of how to use the condition?

Hi James,

you could try something like this maybe :


Company::model()->findAll(array(

   'condition' => 'id = :ID or parent_id = :PARENT_ID',

   'params' => array(

   	':ID' => 1,

   	':PARENT_ID' => 1

   )

));

…or …


Company::model()->findAll(

     'id = :ID or parent_id = :PARENT_ID',

	array(

   	':ID' => 1,

   	':PARENT_ID' => 1

   )

));

Take a look to here…

Hope this helps …

8)

Its works, thank you.

Hi

I have field

employee_id | user_name

1 | pandiamal@gmail.com

2 | xxxxx@yahoo.com

i need employee_id equalend user_name …

i use FindByAttributes() but only getting Array

help me any one

thanks

findByAttributes() returns either found record either NULL. First of all check you didn’t accidentally use findAllByAttributes()

docs

It’s very necessary to clarify AR functions below:




find($condition='',$params=array())

findByAttributes($attributes,$condition='',$params=array())

findByPk($pk,$condition='',$params=array())

findBySql($sql,$params=array())

findAll($condition='',$params=array())

findAllByAttributes($attributes,$condition='',$params=array())

findAllByPk($pk,$condition='',$params=array())

findAllBySql($sql,$params=array())



First of all you need to keep in mind is "find" functions return CActiveRecord, but "findAll" functions return array of records found. If not found, "find" functions return NULL, and "findAll" functions return an empty array.

All "find/findAll" functions can be equally but for flexible usage depends you like.

@param mixed $condition can be a query condition or criteria instance.

If it’s a string, then it will be treated as query condition (in the WHERE clause);

If it’s an array, then it will be treated as the initial values for constructing a {@link CDbCriteria} object;

Otherwise, it should be an instance of {@link CDbCriteria}.

@param array $params are parameters to be bound to an SQL statement.

This is only used when the first parameter is a string (query condition).

In other cases, it’s better to use {@link CDbCriteria::params} to set parameters.

Both $condition and $params are optional, not required. But below parameters is required when you use "ByPk/ByAttributes/BySql" functions.

@param mixed $pk means primary key value(s). If the database table is using multiple primary keys, then you can use array for $pk. For composite key, each key value must be an array (column name=>column value).

@param array $attributes list of attribute values, (indexed by attribute names) that the active records should match. The attribute can be primary key, and an attribute value can be an array which will be used to generate an IN condition.

@param string $sql is a SQL statement. If you don’t like CDbCriteria, and more like use SQL statement, then BySql is a good choice.

Most people like examples:

  1. The code return primary key user_id = a certain value.



$model=User::model()->with('position','userstatus')->find("user_id=$id")

if($model===null)

{

	throw new CHttpException(404,'The requested active record does not exist.');

}

return $model;



  1. You will feel it’s better to use ByPk



$model=User::model()->with('position','userstatus')->findByPk($id);


if($model===null)

{

	throw new CHttpException(404,'The requested active record does not exist.');

}

return $model;



  1. But sometimes, you want to add other attributes into your search, for example you want to use "OR" in the condition



$model=User::model()->with('position','userstatus')->findByAttributes(array(),

'user_id=:userId OR manager_id=:managerId', array(':userId' => $id,        ':managerId' => $id));


if($model===null)

{

	throw new CHttpException(404,'The requested active record does not exist.');

}

return $model;



  1. You don’t want to use criteria to set “select”, and fell more comfortable on sql statement. You can use below example, but remember BySql, you have to write the whole sql statement:



$model=User::model()->with('position','userstatus')->findBySql("SELECT *, DATE_FORMAT(user_start_date,'%m/%d/%Y') AS user_start_date, DATE_FORMAT(user_end_date,'%m/%d/%Y') AS user_end_date, DATE_FORMAT(user_created,'%m/%d/%Y') AS user_created FROM user WHERE user_id=?", array($id));


if($model===null)

{

	throw new CHttpException(404,'The requested active record does not exist.');

}

return $model;



1 Like

what abot findAllByAttribute??Can u pllzzzz explain??