Complete example on findByAttributes in Yii
I am appreciated if someone shows a complete example on how to use findByAttributes in Yii framework.
Official doc @ http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findByAttributes-detail
public CActiveRecord findByAttributes(array $attributes, mixed $condition='', array $params=array ( ))
I know how to use $attributes. I do not see a good reason to use $condition and $params in findByAttributes.
Please tell me under what scenario, you use them in findByAttributes.
Tha开发者_JS百科nk you
It seems like it was included to match similar function in other frameworks and best used when your query is mostly an array of column names like array('dept_id'=>2,'active'=>'y')
-- there might be times in your program where it's easier to pass an array in this format rather than having to re-write them into conditions. You might want to add additional conditions in the $condition paramater or ones that $attributes may not support like project_name LIKE "foo%"
.
$params are used here like other query types, to bind condition values to column types for filtering.
see: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#reading-record
Info: When a query condition is about matching some columns with the specified values, we can use findByAttributes(). We let the $attributes parameters be an array of the values indexed by the column names. In some frameworks, this task can be achieved by calling methods like findByNameAndTitle. Although this approach looks attractive, it often causes confusion, conflict and issues like case-sensitivity of column names.
example of findByAttributes() :
$modelname->findByAttributes(array('fieldName1'=>$value1,'fieldName2'=>$value2));
精彩评论