Skip to content

Commit

Permalink
Enhancing extensibility of CActive* class hierarchy: CActiveFinder an…
Browse files Browse the repository at this point in the history
…d CActiveRecord::model factory methods

Author:    denisarius <denisarius@gmail.com>
  • Loading branch information
denisarius authored and den committed Dec 17, 2012
1 parent 914b9ce commit 154d903
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -103,6 +103,7 @@ Version 1.1.13 work in progress
- Enh #1596: Added CGridView::rowHtmlOptionsExpression to allow set HTML attributes for the row (Ryadnov)
- Enh #1657: CDbCommandBuilder::createUpdateCounterCommand now can be used with float values (samdark, hyzhakus)
- Enh #1658: CFormatter::formatHtml() is now more flexible and customizable through new CFormatter::$htmlPurifierOptions property (resurtm)
- Enh #1863: Enhancing extensibility of CActive* class hierarchy
- Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba)
- Enh: Requirements checker: added check for Oracle database (pdo_oci extension) and MSSQL (pdo_dblib, pdo_sqlsrv and pdo_mssql extensions) (resurtm)
- Enh: Added CChainedLogFilter class to allow adding multiple filters to a logroute (cebe)
Expand Down
17 changes: 13 additions & 4 deletions framework/db/ar/CActiveFinder.php
Expand Up @@ -169,6 +169,15 @@ public function lazyFind($baseRecord)
$this->destroyJoinTree();
}

/** CActiveRecord::model factory method. You may override this method to use your CActive* parallel class hierarchy
* @param string active record class name.
* @return CActiveRecord active record model instance.
*/
public function getModel($className)
{
return CActiveRecord::model($className);
}

private function destroyJoinTree()
{
if($this->_joinTree!==null)
Expand Down Expand Up @@ -212,7 +221,7 @@ private function buildJoinTree($parent,$with,$options=null)
array('{class}'=>get_class($parent->model), '{name}'=>$with)));

$relation=clone $relation;
$model=CActiveRecord::model($relation->className);
$model=$this->getModel($relation->className);

if($relation instanceof CActiveRelation)
{
Expand Down Expand Up @@ -361,7 +370,7 @@ public function __construct($finder,$relation,$parent=null,$id=0)
{
$this->relation=$relation;
$this->_parent=$parent;
$this->model=CActiveRecord::model($relation->className);
$this->model=$this->_finder->getModel($relation->className);
$this->_builder=$this->model->getCommandBuilder();
$this->tableAlias=$relation->alias===null?$relation->name:$relation->alias;
$this->rawTableAlias=$this->_builder->getSchema()->quoteTableName($this->tableAlias);
Expand Down Expand Up @@ -1344,7 +1353,7 @@ public function query()
private function queryOneMany()
{
$relation=$this->relation;
$model=CActiveRecord::model($relation->className);
$model=$this->_finder->getModel($relation->className);
$builder=$model->getCommandBuilder();
$schema=$builder->getSchema();
$table=$model->getTableSchema();
Expand Down Expand Up @@ -1455,7 +1464,7 @@ private function queryOneMany()
private function queryManyMany($joinTableName,$keys)
{
$relation=$this->relation;
$model=CActiveRecord::model($relation->className);
$model=$this->_finder->getModel($relation->className);
$table=$model->getTableSchema();
$builder=$model->getCommandBuilder();
$schema=$builder->getSchema();
Expand Down
23 changes: 16 additions & 7 deletions framework/db/ar/CActiveRecord.php
Expand Up @@ -268,7 +268,7 @@ public function getRelated($name,$refresh=false,$params=array())
$r=$name;
unset($this->_related[$name]);

$finder=new CActiveFinder($this,$r);
$finder=$this->getActiveFinder($r);
$finder->lazyFind($this);

if(!isset($this->_related[$name]))
Expand Down Expand Up @@ -885,6 +885,15 @@ public function onAfterFind($event)
$this->raiseEvent('onAfterFind',$event);
}

/** Factory method to get instance of CActiveFinder. You may override this method to use your CActive* parallel class hierarchy
* @param mixed $with the relation names to be actively looked for
* @return CActiveFinder active finder for the operation
*/
public function getActiveFinder($with)
{
return new CActiveFinder($this, $with);
}

/**
* This method is invoked before saving a record (after validation, if any).
* The default implementation raises the {@link onBeforeSave} event.
Expand Down Expand Up @@ -1298,7 +1307,7 @@ protected function query($criteria,$all=false)
}
else
{
$finder=new CActiveFinder($this,$criteria->with);
$finder=$this->getActiveFinder($criteria->with);
return $finder->query($criteria,$all);
}
}
Expand Down Expand Up @@ -1495,7 +1504,7 @@ public function findBySql($sql,$params=array())
if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
{
$this->resetScope(false);
$finder=new CActiveFinder($this,$criteria->with);
$finder=$this->getActiveFinder($criteria->with);
return $finder->findBySql($sql,$params);
}
else
Expand All @@ -1518,7 +1527,7 @@ public function findAllBySql($sql,$params=array())
if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
{
$this->resetScope(false);
$finder=new CActiveFinder($this,$criteria->with);
$finder=$this->getActiveFinder($criteria->with);
return $finder->findAllBySql($sql,$params);
}
else
Expand Down Expand Up @@ -1546,7 +1555,7 @@ public function count($condition='',$params=array())
return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();
else
{
$finder=new CActiveFinder($this,$criteria->with);
$finder=$this->getActiveFinder($criteria->with);
return $finder->count($criteria);
}
}
Expand All @@ -1573,7 +1582,7 @@ public function countByAttributes($attributes,$condition='',$params=array())
return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();
else
{
$finder=new CActiveFinder($this,$criteria->with);
$finder=$this->getActiveFinder($criteria->with);
return $finder->count($criteria);
}
}
Expand Down Expand Up @@ -1614,7 +1623,7 @@ public function exists($condition='',$params=array())
else
{
$criteria->select='*';
$finder=new CActiveFinder($this,$criteria->with);
$finder=$this->getActiveFinder($criteria->with);
return $finder->count($criteria)>0;
}
}
Expand Down
12 changes: 11 additions & 1 deletion framework/validators/CExistValidator.php
Expand Up @@ -74,7 +74,7 @@ protected function validateAttribute($object,$attribute)

$className=$this->className===null?get_class($object):Yii::import($this->className);
$attributeName=$this->attributeName===null?$attribute:$this->attributeName;
$finder=CActiveRecord::model($className);
$finder=$this->getModel($className);
$table=$finder->getTableSchema();
if(($column=$table->getColumn($attributeName))===null)
throw new CException(Yii::t('yii','Table "{table}" does not have a column named "{column}".',
Expand All @@ -95,5 +95,15 @@ protected function validateAttribute($object,$attribute)
$this->addError($object,$attribute,$message,array('{value}'=>CHtml::encode($value)));
}
}

/** CActiveRecord::model factory method. You may override this method to use your CActive* parallel class hierarchy
* @param string active record class name.
* @return CActiveRecord active record model instance.
*/
protected function getModel($className)
{
return CActiveRecord::model($className);
}

}

11 changes: 10 additions & 1 deletion framework/validators/CUniqueValidator.php
Expand Up @@ -84,7 +84,7 @@ protected function validateAttribute($object,$attribute)

$className=$this->className===null?get_class($object):Yii::import($this->className);
$attributeName=$this->attributeName===null?$attribute:$this->attributeName;
$finder=CActiveRecord::model($className);
$finder=$this->getModel($className);
$table=$finder->getTableSchema();
if(($column=$table->getColumn($attributeName))===null)
throw new CException(Yii::t('yii','Table "{table}" does not have a column named "{column}".',
Expand Down Expand Up @@ -126,5 +126,14 @@ protected function validateAttribute($object,$attribute)
$this->addError($object,$attribute,$message,array('{value}'=>CHtml::encode($value)));
}
}

/** CActiveRecord::model factory method. You may override this method to use your CActive* parallel class hierarchy
* @param string active record class name.
* @return CActiveRecord active record model instance.
*/
protected function getModel($className)
{
return CActiveRecord::model($className);
}
}

11 changes: 10 additions & 1 deletion framework/web/CActiveDataProvider.php
Expand Up @@ -71,7 +71,7 @@ public function __construct($modelClass,$config=array())
if(is_string($modelClass))
{
$this->modelClass=$modelClass;
$this->model=CActiveRecord::model($this->modelClass);
$this->model=$this->getModel($this->modelClass);
}
elseif($modelClass instanceof CActiveRecord)
{
Expand Down Expand Up @@ -116,6 +116,15 @@ public function getSort($className='CSort')
return $sort;
}

/** CActiveRecord::model factory method. You may override this method to use your CActive* parallel class hierarchy
* @param string active record class name.
* @return CActiveRecord active record model instance.
*/
protected function getModel($className)
{
return CActiveRecord::model($className);
}

/**
* Fetches the data from the persistent data storage.
* @return array list of data items
Expand Down
19 changes: 14 additions & 5 deletions framework/web/CSort.php
Expand Up @@ -237,7 +237,7 @@ public function getOrderBy($criteria=null)
else
{
if($this->modelClass!==null)
$schema=CActiveRecord::model($this->modelClass)->getDbConnection()->getSchema();
$schema=$this->getModel($this->modelClass)->getDbConnection()->getSchema();
$orders=array();
foreach($directions as $attribute=>$descending)
{
Expand All @@ -257,7 +257,7 @@ public function getOrderBy($criteria=null)
if(($pos=strpos($attribute,'.'))!==false)
$attribute=$schema->quoteTableName(substr($attribute,0,$pos)).'.'.$schema->quoteColumnName(substr($attribute,$pos+1));
else
$attribute=($criteria===null || $criteria->alias===null ? CActiveRecord::model($this->modelClass)->getTableAlias(true) : $schema->quoteTableName($criteria->alias)).'.'.$schema->quoteColumnName($attribute);
$attribute=($criteria===null || $criteria->alias===null ? $this->getModel($this->modelClass)->getTableAlias(true) : $schema->quoteTableName($criteria->alias)).'.'.$schema->quoteColumnName($attribute);
}
$orders[]=$descending?$attribute.' DESC':$attribute;
}
Expand Down Expand Up @@ -327,7 +327,7 @@ public function resolveLabel($attribute)
elseif(is_string($definition))
$attribute=$definition;
if($this->modelClass!==null)
return CActiveRecord::model($this->modelClass)->getAttributeLabel($attribute);
return $this->getModel($this->modelClass)->getAttributeLabel($attribute);
else
return $attribute;
}
Expand Down Expand Up @@ -422,7 +422,7 @@ public function resolveAttribute($attribute)
if($this->attributes!==array())
$attributes=$this->attributes;
elseif($this->modelClass!==null)
$attributes=CActiveRecord::model($this->modelClass)->attributeNames();
$attributes=$this->getModel($this->modelClass)->attributeNames();
else
return false;
foreach($attributes as $name=>$definition)
Expand All @@ -434,7 +434,7 @@ public function resolveAttribute($attribute)
}
elseif($definition==='*')
{
if($this->modelClass!==null && CActiveRecord::model($this->modelClass)->hasAttribute($attribute))
if($this->modelClass!==null && $this->getModel($this->modelClass)->hasAttribute($attribute))
return $attribute;
}
elseif($definition===$attribute)
Expand All @@ -443,6 +443,15 @@ public function resolveAttribute($attribute)
return false;
}

/** CActiveRecord::model factory method. You may override this method to use your CActive* parallel class hierarchy
* @param string active record class name.
* @return CActiveRecord active record model instance.
*/
protected function getModel($className)
{
return CActiveRecord::model($className);
}

/**
* Creates a hyperlink based on the given label and URL.
* You may override this method to customize the link generation.
Expand Down

0 comments on commit 154d903

Please sign in to comment.