SmartAR.php 7.06 KB
Newer Older
irving's avatar
irving committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
<?php
/**
* SmartAR class file
* 
* @author Vitaliy Stepanenko <mail@vitaliy.in>
* @package SmartActiveRecord
* @version $Id: SmartAR.php 29314 2011-06-10 15:38:17Z stepanenko $
* @license BSD
* 
* @todo check populateRecords
* 
*/

/**
* This class gives possibility to use power of Yii ActiveRecord without describing new model classes.
* It can be useful when you work with dynamically generated tables in database, 
* or when you don't want to create a lot of code to do some simple operations using convenient ActiveRecord interface.
* 
* <p><b>=== Usage ===</b>:</p>
* <code>
* <?php
* 	Yii::import('ext.components.SmartActiveRecord.SmartAR');
*	$model = SmartAR::model('tbl_MyTable')->findByAttributes('id'=>2);
* 	$model->somefield = someValue;
* 	$model->save();
* 	
* ?>
* </code> 
* 
* <p><b>=== Some logics of creating models ===</b>:</p>
* SAR::create(config)
*     $instance->new SAR, AR::__construct(null)
* 	  SAR::$_lastTableName	== config.tableName
*     $instance->AR::refreshMetaData
* 	  	AR::model(get_class($this)==SAR)
* 			AR::$_models['SAR'] = new SAR, AR::__construct(null)			
* 		AR::$_models['SAR']->metadata = new metadata(AR::$_models['SAR']->tableName() ====> SAR::$_lastTableName) //SQL query?
*       $instance->metadata = AR::$_models['SAR']->metadata ====> new metadata(SAR::$_lastTableName)
* --> configured instance is not pushed fo SAR::$_models[tableName], SAR::$_models[tableName] is null, SAR::$_models[tableName] is not used.
* 
* 
*/
class SmartAR extends CActiveRecord implements IApplicationComponent
{
	
	const USE_STATIC_MODEL = 0;
	
	private $_initialized = false;
	
	/**
	* Static models
	* 
	* @var array
	*/
	protected static $_models;	
	
	/**
	* Table name stores here intermediately after SmartAR::model() and before first call of SmartAR::tableName
	* 
	* @var string
	*/
	protected static $_lastTableName;
	
	/**
	* Table name
	* 
	* @var string
	*/
	protected $_tableName;
	
	/**
	* list of related object declarations
	* 
	* @var array
	*/
	public $relations = array();
	public $scopes = array();
	public $rules = array();
	public $behaviors = array();
	public $attributeLabels = array();
	private $_setOnlySafeAttributes = false;
	
	/**
	* Returns the static model (instance of SmartAR) for specified DB table.		
	* @param string $tableName db table name
	* @return SmartAR active record model instance.
	*/
	public static function model($tableName)
	{		        
		/** @todo replace self::* to static::* when we will use only PHP 5.3+ that have a support of LSB */
		if(!isset(self::$_models[$tableName])) {
			self::create($tableName,true);
		}
		return self::$_models[$tableName];
	}
	
	/**
	* Factory method that creates model.
	* To prevent metadata refreshing use static model instead of this method if it is possible.
	* This method can be useful when you use named scopes, have alot of work with AR DB criteria, etc.
	* 
	* @todo test it
	* 
	* @param string $tableName
	* @return SmartAR
	*/
	public static function create($config, $useAsStaticModel=false)
	{
		if (is_string($config)) {
			$model = new SmartAR(null);
			$model->setTableName($config);	
		} else {
			$model = Yii::createComponent($config,null);
		}	
		
		if (!$useAsStaticModel) {
			$model->useStaticModelOptions();
		} else {
			self::$_models[$config] = $model;//@todo!!!
		}					
		
		$model->init();
		return $model;
	}
	
	/**
	* Deny instantiating not via SmartAR::create
	* 
	* @param mixed $scenario
	* @return SmartAR
	*/
	public function __construct($scenario = 'insert') {
		if ($scenario!==null) {
			throw new CException('SmartAR class don\'t allow instantiating objects,  you must use SmartAR::create("<tableName|config>") fabric method instead of it.');
		}
	}
	
	public function setTableName($tableName)
	{
	   $this->_tableName  = self::$_lastTableName = $tableName;
	   $this->refreshMetaData();
	   self::$_lastTableName = null;
	}
	
		
	
	/**
	* Options from static model will used instead of instance options
	* @return SmartAR
	* 
	*/
	protected function useStaticModelOptions()
	{
		$this->relations 
		= $this->scopes 
		= $this->rules 
		= $this->behaviors
		= $this->attributeLabels 
		= $this->_setOnlySafeAttributes = self::USE_STATIC_MODEL;
		return $this;		
	}
	
	
	/**
	* Configure model instance options.
	* It's most convenient way of configuring static models (simple model instace we can configure when creating)
	* 
	* @param array $config
	* @return SmartAR
	*/
	public function configure($config)
	{		
		if (isset($config['class'])) {
			unset($config['class']);
		}
			
		foreach ($config as $field=>$value) {
			$this->$field = $value;
		}
		
		return $this;
	}
	  
	/**
	* Mark all table columns as safe attributes by default.
	* @return array list of safe attributes (equal to table columns).
	* @todo do something with it, coz it's security fail
	*/
	public function getSafeAttributeNames()
	{
		if ($this->setOnlySafeAttributes) {
			return parent::getSafeAttributeNames();
		} else {
			return array_keys($this->getMetaData()->columns);	
		}
	}
	
	public function setAttributes($values, $safeOnly=null)
	{
		 if ($safeOnly === null) {
			 $safeOnly = $this->setOnlySafeAttributes;
		 }
		 return parent::setAttributes($values, $safeOnly);
	}
	
	public function setSetOnlySafeAttributes($value) 
	{
		$this->_setOnlySafeAttributes = $value;
	}
	
 	public function getSetOnlySafeAttributes() 
 	{
		if ($this->_setOnlySafeAttributes === self::USE_STATIC_MODEL) {
			return self::model($this->tableName())->setOnlySafeAttributes;
		}
		return $this->_setOnlySafeAttributes;
	}
	
	public function init()
	{								
		$this->attachBehaviors($this->behaviors());
		$this->afterConstruct();
		$this->_initialized = true;
	}
	
	public function getIsInitialized()
	{
		return $this->_initialized;
	}
	
	/**
	* @return name of DB table that is associated with this instance of SmartAR.
	*/
	public function tableName()
	{			
		if ($this->_tableName) 		return $this->_tableName;	   	
		if (self::$_lastTableName) 	return self::$_lastTableName;
		else throw new CException('Table name not specified.');
	}	
	
	public function relations()
	{
		if ($this->relations === self::USE_STATIC_MODEL) {
			return self::model($this->tableName())->relations;
		}
		return $this->relations;
	}
	
	public function scopes()
	{
		if ($this->scopes === self::USE_STATIC_MODEL) {
			return self::model($this->tableName())->scopes;
		}
		return $this->scopes;
	}
	
	public function rules()
	{
		if ($this->rules === self::USE_STATIC_MODEL) {
			return self::model($this->tableName())->rules;
		}
		return $this->rules;
	}
	
	public function behaviors()
	{
		if ($this->behaviors === self::USE_STATIC_MODEL) {
			return self::model($this->tableName())->behaviors;
		}		
		return $this->behaviors;
	}
	
	public function attributeLabels()
	{
		if ($this->attributeLabels === self::USE_STATIC_MODEL) {
			return self::model($this->tableName())->attributeLabels;
		}	
		return $this->attributeLabels;
	}   
}