Last active
December 17, 2015 17:59
-
-
Save JiLiZART/5650121 to your computer and use it in GitHub Desktop.
Extends base yii CActiveRecord, for return results as array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class EActiveRecordAsArrayBehavior | |
* @method CActiveRecord getOwner() | |
*/ | |
class EActiveRecordAsArrayBehavior extends CActiveRecordBehavior | |
{ | |
/** | |
* Finds all active records satisfying the specified condition. | |
* See {@link find()} for detailed explanation about $condition and $params. | |
* @param mixed $condition query condition or criteria. | |
* @param array $params parameters to be bound to an SQL statement. | |
* @return array[] list of active records satisfying the specified condition. An empty array is returned if none is found. | |
* @see CActiveRecord::findAll() | |
*/ | |
public function findAllAsArray($condition='', $params=array()) | |
{ | |
Yii::trace(get_class($this).'.findAllAsArray()','EActiveRecordAsArrayBehavior'); | |
$criteria=$this->getOwner()->getCommandBuilder()->createCriteria($condition,$params); | |
return $this->query($criteria,true); | |
} | |
/** | |
* Finds a single active record with the specified condition. | |
* @param mixed $condition query condition or criteria. | |
* If a string, it is treated as query condition (the WHERE clause); | |
* If an array, it is treated as the initial values for constructing a {@link CDbCriteria} object; | |
* Otherwise, it should be an instance of {@link CDbCriteria}. | |
* @param array $params parameters to be bound to an SQL statement. | |
* This is only used when the first parameter is a string (query condition). | |
* In other cases, please use {@link CDbCriteria::params} to set parameters. | |
* @return array the record found. An empty array is returned if none is found. | |
* @see CActiveRecord::find() | |
*/ | |
public function findAsArray($condition='', $params=array()) | |
{ | |
Yii::trace(get_class($this).'.findAsArray()','EActiveRecordAsArrayBehavior'); | |
$criteria=$this->getOwner()->getCommandBuilder()->createCriteria($condition,$params); | |
return $this->query($criteria); | |
} | |
/** | |
* Performs the actual DB query and populates the AR objects with the query result. | |
* This method is mainly internally used by other AR query methods. | |
* @param CDbCriteria $criteria the query criteria | |
* @param boolean $all whether to return all data | |
* @return mixed the AR objects populated with the query result | |
* @since 1.1.7 | |
* @see CActiveRecord::query() | |
*/ | |
protected function query($criteria,$all=false) | |
{ | |
$this->getOwner()->applyScopes($criteria); | |
if(!$all) | |
$criteria->limit=1; | |
$command=$this->getOwner()->getCommandBuilder()->createFindCommand($this->getOwner()->getTableSchema(),$criteria,$this->getOwner()->getTableAlias()); | |
return $all ? $command->queryAll() : $command->queryRow(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment