Friday, March 30, 2012

PHP Yii ActiveRecord findAllByAttributes()

Definition:
public array findAllByAttributes(array $attributes, mixed $condition='', array $params=array ( ))
(refer http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAllByAttributes-detail)


Explanation:
Finds all active records that have specific attribute values


Example:
1. $model = MyModel::model()->findAllByAttributes(array('id'=>1, 'colA'='mystring'));
2. $model = MyModel::model()->findAllByAttributes(array('id'=>1, 'colA'='mystring'), "colB='mystring2'");
3. $model = MyModel::model()->findAllByAttributes(array('id'=>1, 'colA'='mystring'), "colB=:param", array(":param"=>'mystring2'));


Internal:

findAllByAttributes($attribute, $condition, $param)
|-$criteria = createColumnCriteria($this->getTableSchema(), $attributes, $condition, $param, $prefix)
  Note: $criteria is a CDbCriteria
           $prefix is defaulted to NULL. When calling findAllByAttributes, this function will not pass in $prefix. Therefore, in this case, $prefix is always NULL.
  |-query($criteria, TRUE)


Thursday, March 29, 2012

PHP Yii ActiveRecord FindAll()

Definition:
public array findAll(mixed $condition='', array $params=array ( ))
(refer http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail)


Explanation:
Find all active records that satisfy specific conditions
findAll() returns array instead of CAtiveRecord


Example:
1. $model = MyModel::model()->findAll("id=1");



Internal:
please refer find() for explanation of $condition.

findAll($condition, $param)
|-$criteria = createCriteria($condition, $param)
  > Note: $criteria is a CDbCriteria
  |-query($criteria, TRUE)
  Note: TRUE has been passed in so that system will retrieve all active records

(refer yii\framework\db\ar\CActiveRecord.php)


Tuesday, March 27, 2012

PHP Yii ActiveRecord Find()

Definition:
public CActiveRecord find(mixed $condition='', array $params=array ( ))

Explanation:
Find a single active record with specified conditions
Always return first record in the result set


$condition can be either
1. array
2. CDbCriteria
3. string

When $condition is an array, CDbCriteria class will map each key to predefined field name. Below is a list of predefined field name:
a. $select [default = '*']
b. $distinct [default = FALSE]
c. $condition [default = '']
d. $param [default = array()]
e. $limit [default = -1]
f. $offset [default = -1]
g. $order [default = '']
h. $group [default = '']
i. $join [default = '']
j. $having [default = '']
k. $with - alias of the table name [default = 't']
l. others - $alias, $together, $index, and $scopes

When $condition is a string, system will map it to CDbCriteria->$condition (item c above)

Example:
1. $model = MyModel::model()->find("id=1");
2. $model = MyModel::model()->find("id={$id}");
3. $model = MyModel::model()->find("value='mystring'");
4. $model = MyModel::model()->find("value='{$mystring}'");
5. $model = MyModel::model()->find("id=1 ORDER BY colA ASC");
6. $model = MyModel::model()->find("id=:param", array(":param"=>1));
7. $model = MyModel::model()->find("id=:param1 AND colA=:param2", array(":param1"=>1, ":param2"=>'mystring'));
8. $model = MyModel::model()->find(array('select'=>'colA', 'condition'=>'id=1', 'order'=>'colB DESC'));

Internal:
find($condition, $param)
|-$criteria = createCriteria($condition, $param)
  Note: $criteria is a CDbCriteria
  |-query($criteria, FALSE)
    Note: FALSE means add "LIMIT 1" to SELECT statement

(refer yii\framework\db\ar\CActiveRecord.php)