Saturday 31 March 2012

YII Framework model findAllByAttributes()

For Yii Framework findAllByAttributes():


Obj::model()->findAllByAttributes($filterArr, $conditionArr);

$filterArr: To generate the SQL conditions in WHERE clause.
                 e.g., array('filter1'=>'v1', 'filter2'=>'v2'...)
$conditionArr: To generate the SQL other conditions (order by, limit...)
                 e.g., array('order'=>'v1 DESC', 'limit'=>'10',...)

e.g., if our SQL is: 

SELECT id FROM user WHERE username = 'jack' ORDER BY creationdate

The following code may achieve this:

$filterArr = array('username'=>'jack');
$conditionArr = array('order'=>'creationdate');
User::model()->findAllByAttributes($filterArr, $conditionArr);


daxue.menggy.com

4 comments:

  1. How to get only id from this syntax.

    $filterArr = array('username'=>'jack');
    $conditionArr = array('order'=>'creationdate');
    User::model()->findAllByAttributes($filterArr, $conditionArr);


    ReplyDelete
    Replies
    1. sorry, you can't. you'd better to use CDbCriteria : http://www.larryullman.com/2013/07/24/using-cdbcriteria-in-the-yii-framework/

      Delete
  2. Superb, fantastically helpful, many thanks.

    ReplyDelete