CJoinElement
鍖 | system.db.ar |
---|---|
缁ф壙 | class CJoinElement |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CActiveFinder.php 3562 2012-02-13 01:27:06Z qiang.xue $ |
婧愮爜 | framework/db/ar/CActiveFinder.php |
CJoinElement琛ㄧず涓涓湪鍏宠仈鏍戜腑鐢CActiveFinder鍒涘缓鐨勫瓙鏍戣妭鐐广
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
children | array | 瀛愬叧鑱斿厓绱犲垪琛 | CJoinElement |
id | integer | 鏍戣妭鐐圭殑鍞竴ID | CJoinElement |
master | CActiveRelation | 涓诲叧绯 | CJoinElement |
model | CActiveRecord | 涓庢爲鑺傜偣鍏宠仈鐨勬ā鍨 | CJoinElement |
rawTableAlias | string | 姝ゅ厓绱犵殑寮曠敤琛ㄧ殑鍒悕 | CJoinElement |
records | array | 姝ゆ煡璇㈡壘鍒扮殑娲诲姩璁板綍鍒楄〃锛屽畠浠寜涓婚敭鍊肩储寮曘 | CJoinElement |
relation | CActiveRelation | 姝ゆ爲鑺傜偣琛ㄧず鐨勫叧绯 | CJoinElement |
slave | CActiveRelation | 浠庡叧绯 | CJoinElement |
stats | array | 鍏冪礌鍒楄〃 | CJoinElement |
tableAlias | string | 鍏宠仈鍏冪礌鐨勮〃鍒悕 | CJoinElement |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__construct() | 鏋勯犲嚱鏁般 | CJoinElement |
afterFind() | 璋冪敤鎵鏈夎褰曠殑 CActiveRecord::afterFind . | CJoinElement |
beforeFind() | 璋冪敤 CActiveRecord::beforeFind. | CJoinElement |
buildQuery() | 寤虹珛鍏宠仈鏌ヨ骞跺甫鏈夋墍鏈夌殑渚濊禆 HAS_ONE 鍜 BELONGS_TO 缁撶偣. | CJoinElement |
count() | 杩斿洖鍏宠仈璇彞鏌ヨ鍑烘潵鐨勪富璁板綍鐨勬暟鐩 | CJoinElement |
destroy() | 鍒犻櫎寮曠敤鐨勫瓙鍏冪礌鍜屾煡璇紝浠ラ伩鍏嶅惊鐜紩鐢ㄣ | CJoinElement |
find() | 鏍规嵁鏌ヨ瀹炰綋鎵ц寰幆杞鏌ユ壘銆 | CJoinElement |
findWithBase() | 褰撳熀鏈褰曞彲鐢ㄧ殑鏃跺欙紝鎵ц棰勫厛鍔犺浇銆 | CJoinElement |
getColumnPrefix() | 杩斿洖鍏宠仈鐨勫瓧娈靛墠缂浠ョ敤鏉ユ秷闄ゆ涔夈 | CJoinElement |
getColumnSelect() | 鐢熸垚瑕佹煡璇㈢殑瀛楁鍒楄〃銆 | CJoinElement |
getJoinCondition() | 杩斿洖杩斿洖鍏宠仈璇彞(杩欎釜缁撶偣涓庡畠鐨勭埗缁撶偣杩炴帴) | CJoinElement |
getPrimaryKeyRange() | 杩斿洖鏍规嵁鏌ヨ鐨勪富閿兼寚瀹氳鐨勬潯浠躲 | CJoinElement |
getPrimaryKeySelect() | 杩斿洖杩斿洖涓婚敭鐨勯夋嫨 | CJoinElement |
getTableNameWithAlias() | 杩斿洖杩斿洖琛ㄥ悕鍜岃〃鍒悕锛堣嫢鏈夛級锛岃繖涓彲浠ユ棤椤昏浆涔夌洿鎺ュ湪SQL鏌ヨ涓娇鐢ㄣ | CJoinElement |
lazyFind() | 鏍规嵁鎸囧畾鐨勫熀璁板綍鏉ユ墽琛屽欢杩熸煡鎵俱 | CJoinElement |
runQuery() | 鎵ц鍏宠仈鏌ヨ骞跺~鍏呮煡璇㈢粨鏋溿 | CJoinElement |
灞炴ц缁
children
灞炴
public array $children;
瀛愬叧鑱斿厓绱犲垪琛
id
灞炴
public integer $id;
鏍戣妭鐐圭殑鍞竴ID
master
灞炴
public CActiveRelation $master;
涓诲叧绯
model
灞炴
public CActiveRecord $model;
涓庢爲鑺傜偣鍏宠仈鐨勬ā鍨
rawTableAlias
灞炴
public string $rawTableAlias;
姝ゅ厓绱犵殑寮曠敤琛ㄧ殑鍒悕
records
灞炴
public array $records;
姝ゆ煡璇㈡壘鍒扮殑娲诲姩璁板綍鍒楄〃锛屽畠浠寜涓婚敭鍊肩储寮曘
relation
灞炴
public CActiveRelation $relation;
姝ゆ爲鑺傜偣琛ㄧず鐨勫叧绯
slave
灞炴
public CActiveRelation $slave;
浠庡叧绯
stats
灞炴
public array $stats;
鍏冪礌鍒楄〃
tableAlias
灞炴
public string $tableAlias;
鍏宠仈鍏冪礌鐨勮〃鍒悕
鏂规硶璇︾粏
__construct()
鏂规硶
public void __construct(CActiveFinder $finder, mixed $relation, CJoinElement $parent=NULL, integer $id=0)
| ||
$finder | CActiveFinder | 鏌ヨ瀵硅薄 |
$relation | mixed | 鍏宠仈鍒拌繖涓爲鑺傜偣鐨勫叧绯伙紙濡傛灉绗笁涓弬鏁颁笉涓 null锛夛紝 鎴栬呮槸妯″瀷锛堝鏋滅涓変釜鍙傛暟涓 null锛夈 |
$parent | CJoinElement | 鐖舵爲鑺傜偣 |
$id | integer | 姝ゆ爲缁撶偣鐨処D锛屽湪鎵鏈夋爲缁撶偣涓槸鍞竴鐨 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#386 (鏄剧ず)
public function __construct($finder,$relation,$parent=null,$id=0)
{
$this->_finder=$finder;
$this->id=$id;
if($parent!==null)
{
$this->relation=$relation;
$this->_parent=$parent;
$this->model=CActiveRecord::model($relation->className);
$this->_builder=$this->model->getCommandBuilder();
$this->tableAlias=$relation->alias===null?$relation->name:$relation->alias;
$this->rawTableAlias=$this->_builder->getSchema()->quoteTableName($this->tableAlias);
$this->_table=$this->model->getTableSchema();
}
else // root element, the first parameter is the model.
{
$this->model=$relation;
$this->_builder=$relation->getCommandBuilder();
$this->_table=$relation->getTableSchema();
$this->tableAlias=$this->model->getTableAlias();
$this->rawTableAlias=$this->_builder->getSchema()->quoteTableName($this->tableAlias);
}
// set up column aliases, such as t1_c2
$table=$this->_table;
if($this->model->getDbConnection()->getDriverName()==='oci') // Issue 482
$prefix='T'.$id.'_C';
else
$prefix='t'.$id.'_c';
foreach($table->getColumnNames() as $key=>$name)
{
$alias=$prefix.$key;
$this->_columnAliases[$name]=$alias;
if($table->primaryKey===$name)
$this->_pkAlias=$alias;
else if(is_array($table->primaryKey) && in_array($name,$table->primaryKey))
$this->_pkAlias[$name]=$alias;
}
}
鏋勯犲嚱鏁般
afterFind()
鏂规硶
public void afterFind()
|
婧愮爜锛 framework/db/ar/CActiveFinder.php#781 (鏄剧ず)
public function afterFind()
{
foreach($this->records as $record)
$record->afterFindInternal();
foreach($this->children as $child)
$child->afterFind();
$this->children = null;
}
璋冪敤鎵鏈夎褰曠殑 CActiveRecord::afterFind .
beforeFind()
鏂规硶
public void beforeFind(boolean $isChild=true)
| ||
$isChild | boolean | 鏄惁浣滀负涓涓瓙鑺傜偣璋冪敤 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#769 (鏄剧ず)
public function beforeFind($isChild=true)
{
if($isChild)
$this->model->beforeFindInternal();
foreach($this->children as $child)
$child->beforeFind(true);
}
璋冪敤 CActiveRecord::beforeFind.
buildQuery()
鏂规硶
public void buildQuery(CJoinQuery $query)
| ||
$query | CJoinQuery | 姝ゆ煡璇㈣寤虹珛 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#795 (鏄剧ず)
public function buildQuery($query)
{
foreach($this->children as $child)
{
if($child->master!==null)
$child->_joined=true;
else if($child->relation instanceof CHasOneRelation || $child->relation instanceof CBelongsToRelation
|| $this->_finder->joinAll || $child->relation->together || (!$this->_finder->baseLimited && $child->relation->together===null))
{
$child->_joined=true;
$query->join($child);
$child->buildQuery($query);
}
}
}
寤虹珛鍏宠仈鏌ヨ骞跺甫鏈夋墍鏈夌殑渚濊禆 HAS_ONE 鍜 BELONGS_TO 缁撶偣.
count()
鏂规硶
public string count(CDbCriteria $criteria=NULL)
| ||
$criteria | CDbCriteria | 鏌ヨ鏉′欢 |
{return} | string | 杩斿洖涓昏褰曠殑鏁伴噺銆 娉ㄦ剰: 绫诲瀷涓哄瓧绗︿覆淇濇寔鏈澶х簿搴︺ |
婧愮爜锛 framework/db/ar/CActiveFinder.php#738 (鏄剧ず)
public function count($criteria=null)
{
$query=new CJoinQuery($this,$criteria);
// ensure only one big join statement is used
$this->_finder->baseLimited=false;
$this->_finder->joinAll=true;
$this->buildQuery($query);
$select=is_array($criteria->select) ? implode(',',$criteria->select) : $criteria->select;
if($select!=='*' && !strncasecmp($select,'count',5))
$query->selects=array($select);
else if(is_string($this->_table->primaryKey))
{
$prefix=$this->getColumnPrefix();
$schema=$this->_builder->getSchema();
$column=$prefix.$schema->quoteColumnName($this->_table->primaryKey);
$query->selects=array("COUNT(DISTINCT $column)");
}
else
$query->selects=array("COUNT(*)");
$query->orders=$query->groups=$query->havings=array();
$query->limit=$query->offset=-1;
$command=$query->createCommand($this->_builder);
return $command->queryScalar();
}
杩斿洖鍏宠仈璇彞鏌ヨ鍑烘潵鐨勪富璁板綍鐨勬暟鐩
destroy()
鏂规硶
public void destroy()
|
婧愮爜锛 framework/db/ar/CActiveFinder.php#430 (鏄剧ず)
public function destroy()
{
if(!empty($this->children))
{
foreach($this->children as $child)
$child->destroy();
}
unset($this->_finder, $this->_parent, $this->model, $this->relation, $this->master, $this->slave, $this->records, $this->children, $this->stats);
}
鍒犻櫎寮曠敤鐨勫瓙鍏冪礌鍜屾煡璇紝浠ラ伩鍏嶅惊鐜紩鐢ㄣ 杩欎釜鏂规硶鏄唴閮ㄤ娇鐢ㄣ
find()
鏂规硶
public void find(CDbCriteria $criteria=NULL)
| ||
$criteria | CDbCriteria | the query criteria |
婧愮爜锛 framework/db/ar/CActiveFinder.php#444 (鏄剧ず)
public function find($criteria=null)
{
if($this->_parent===null) // root element
{
$query=new CJoinQuery($this,$criteria);
$this->_finder->baseLimited=($criteria->offset>=0 || $criteria->limit>=0);
$this->buildQuery($query);
$this->_finder->baseLimited=false;
$this->runQuery($query);
}
else if(!$this->_joined && !empty($this->_parent->records)) // not joined before
{
$query=new CJoinQuery($this->_parent);
$this->_joined=true;
$query->join($this);
$this->buildQuery($query);
$this->_parent->runQuery($query);
}
foreach($this->children as $child) // find recursively
$child->find();
foreach($this->stats as $stat)
$stat->query();
}
鏍规嵁鏌ヨ瀹炰綋鎵ц寰幆杞鏌ユ壘銆
findWithBase()
鏂规硶
public void findWithBase(mixed $baseRecords)
| ||
$baseRecords | mixed | 鍙敤鐨勫熀鏈褰曘 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#702 (鏄剧ず)
public function findWithBase($baseRecords)
{
if(!is_array($baseRecords))
$baseRecords=array($baseRecords);
if(is_string($this->_table->primaryKey))
{
foreach($baseRecords as $baseRecord)
$this->records[$baseRecord->{$this->_table->primaryKey}]=$baseRecord;
}
else
{
foreach($baseRecords as $baseRecord)
{
$pk=array();
foreach($this->_table->primaryKey as $name)
$pk[$name]=$baseRecord->$name;
$this->records[serialize($pk)]=$baseRecord;
}
}
$query=new CJoinQuery($this);
$this->buildQuery($query);
if(count($query->joins)>1)
$this->runQuery($query);
foreach($this->children as $child)
$child->find();
foreach($this->stats as $stat)
$stat->query();
}
褰撳熀鏈褰曞彲鐢ㄧ殑鏃跺欙紝鎵ц棰勫厛鍔犺浇銆
getColumnPrefix()
鏂规硶
public string getColumnPrefix()
| ||
{return} | string | 鍏宠仈鐨勫瓧娈靛墠缂浠ョ敤鏉ユ秷闄ゆ涔夈 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#1028 (鏄剧ず)
public function getColumnPrefix()
{
if($this->tableAlias!==null)
return $this->rawTableAlias.'.';
else
return $this->_table->rawName.'.';
}
getColumnSelect()
鏂规硶
public string getColumnSelect(mixed $select='*')
| ||
$select | mixed | 瑕佹煡璇㈢殑瀛楁锛岄粯璁ゅ间负 '*', 鎸囩ず鎵鏈夌殑瀛楁銆 |
{return} | string | 杩斿洖瀛楁閫夋嫨 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#919 (鏄剧ず)
public function getColumnSelect($select='*')
{
$schema=$this->_builder->getSchema();
$prefix=$this->getColumnPrefix();
$columns=array();
if($select==='*')
{
foreach($this->_table->getColumnNames() as $name)
$columns[]=$prefix.$schema->quoteColumnName($name).' AS '.$schema->quoteColumnName($this->_columnAliases[$name]);
}
else
{
if(is_string($select))
$select=explode(',',$select);
$selected=array();
foreach($select as $name)
{
$name=trim($name);
$matches=array();
if(($pos=strrpos($name,'.'))!==false)
$key=substr($name,$pos+1);
else
$key=$name;
$key=trim($key,'\'"`');
if($key==='*')
{
foreach($this->_table->columns as $name=>$column)
{
$alias=$this->_columnAliases[$name];
if(!isset($selected[$alias]))
{
$columns[]=$prefix.$column->rawName.' AS '.$schema->quoteColumnName($alias);
$selected[$alias]=1;
}
}
continue;
}
if(isset($this->_columnAliases[$key])) // simple column names
{
$columns[]=$prefix.$schema->quoteColumnName($key).' AS '.$schema->quoteColumnName($this->_columnAliases[$key]);
$selected[$this->_columnAliases[$key]]=1;
}
else if(preg_match('/^(.*?)\s+AS\s+(\w+)$/im',$name,$matches)) // if the column is already aliased
{
$alias=$matches[2];
if(!isset($this->_columnAliases[$alias]) || $this->_columnAliases[$alias]!==$alias)
{
$this->_columnAliases[$alias]=$alias;
$columns[]=$name;
$selected[$alias]=1;
}
}
else
throw new CDbException(Yii::t('yii','Active record "{class}" is trying to select an invalid column "{column}". Note, the column must exist in the table or be an expression with alias.',
array('{class}'=>get_class($this->model), '{column}'=>$name)));
}
// add primary key selection if they are not selected
if(is_string($this->_pkAlias) && !isset($selected[$this->_pkAlias]))
$columns[]=$prefix.$schema->quoteColumnName($this->_table->primaryKey).' AS '.$schema->quoteColumnName($this->_pkAlias);
else if(is_array($this->_pkAlias))
{
foreach($this->_table->primaryKey as $name)
if(!isset($selected[$name]))
$columns[]=$prefix.$schema->quoteColumnName($name).' AS '.$schema->quoteColumnName($this->_pkAlias[$name]);
}
}
return implode(', ',$columns);
}
鐢熸垚瑕佹煡璇㈢殑瀛楁鍒楄〃銆 瀛楁浼氭纭鐞嗗埆鍚嶏紝濡傛灉娌℃湁鎸囧畾涓婚敭锛岄偅涔堜細鑷姩灏嗕富閿姞鍒版煡璇㈣鍙ャ
getJoinCondition()
鏂规硶
public string getJoinCondition()
| ||
{return} | string | 杩斿洖鍏宠仈璇彞(杩欎釜缁撶偣涓庡畠鐨勭埗缁撶偣杩炴帴) |
婧愮爜锛 framework/db/ar/CActiveFinder.php#1039 (鏄剧ず)
public function getJoinCondition()
{
$parent=$this->_parent;
if($this->relation instanceof CManyManyRelation)
{
if(!preg_match('/^\s*(.*?)\((.*)\)\s*$/',$this->relation->foreignKey,$matches))
throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key. The format of the foreign key must be "joinTable(fk1,fk2,...)".',
array('{class}'=>get_class($parent->model),'{relation}'=>$this->relation->name)));
$schema=$this->_builder->getSchema();
if(($joinTable=$schema->getTable($matches[1]))===null)
throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is not specified correctly: the join table "{joinTable}" given in the foreign key cannot be found in the database.',
array('{class}'=>get_class($parent->model), '{relation}'=>$this->relation->name, '{joinTable}'=>$matches[1])));
$fks=preg_split('/\s*,\s*/',$matches[2],-1,PREG_SPLIT_NO_EMPTY);
return $this->joinManyMany($joinTable,$fks,$parent);
}
else
{
$fks=is_array($this->relation->foreignKey) ? $this->relation->foreignKey : preg_split('/\s*,\s*/',$this->relation->foreignKey,-1,PREG_SPLIT_NO_EMPTY);
if($this->relation instanceof CBelongsToRelation)
{
$pke=$this;
$fke=$parent;
}
else if($this->slave===null)
{
$pke=$parent;
$fke=$this;
}
else
{
$pke=$this;
$fke=$this->slave;
}
return $this->joinOneMany($fke,$fks,$pke,$parent);
}
}
getPrimaryKeyRange()
鏂规硶
public string getPrimaryKeyRange()
| ||
{return} | string | 鏍规嵁鏌ヨ鐨勪富閿兼寚瀹氳鐨勬潯浠躲 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#1012 (鏄剧ず)
public function getPrimaryKeyRange()
{
if(empty($this->records))
return '';
$values=array_keys($this->records);
if(is_array($this->_table->primaryKey))
{
foreach($values as &$value)
$value=unserialize($value);
}
return $this->_builder->createInCondition($this->_table,$this->_table->primaryKey,$values,$this->getColumnPrefix());
}
getPrimaryKeySelect()
鏂规硶
public string getPrimaryKeySelect()
| ||
{return} | string | 杩斿洖涓婚敭鐨勯夋嫨 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#994 (鏄剧ず)
public function getPrimaryKeySelect()
{
$schema=$this->_builder->getSchema();
$prefix=$this->getColumnPrefix();
$columns=array();
if(is_string($this->_pkAlias))
$columns[]=$prefix.$schema->quoteColumnName($this->_table->primaryKey).' AS '.$schema->quoteColumnName($this->_pkAlias);
else if(is_array($this->_pkAlias))
{
foreach($this->_pkAlias as $name=>$alias)
$columns[]=$prefix.$schema->quoteColumnName($name).' AS '.$schema->quoteColumnName($alias);
}
return implode(', ',$columns);
}
getTableNameWithAlias()
鏂规硶
public string getTableNameWithAlias()
| ||
{return} | string | 杩斿洖琛ㄥ悕鍜岃〃鍒悕锛堣嫢鏈夛級锛岃繖涓彲浠ユ棤椤昏浆涔夌洿鎺ュ湪SQL鏌ヨ涓娇鐢ㄣ |
婧愮爜锛 framework/db/ar/CActiveFinder.php#905 (鏄剧ず)
public function getTableNameWithAlias()
{
if($this->tableAlias!==null)
return $this->_table->rawName . ' ' . $this->rawTableAlias;
else
return $this->_table->rawName;
}
lazyFind()
鏂规硶
public void lazyFind(CActiveRecord $baseRecord)
| ||
$baseRecord | CActiveRecord | 瑕佽鑾峰彇娲诲姩璁板綍鐨勭浉鍏冲璞 |
婧愮爜锛 framework/db/ar/CActiveFinder.php#474 (鏄剧ず)
public function lazyFind($baseRecord)
{
if(is_string($this->_table->primaryKey))
$this->records[$baseRecord->{$this->_table->primaryKey}]=$baseRecord;
else
{
$pk=array();
foreach($this->_table->primaryKey as $name)
$pk[$name]=$baseRecord->$name;
$this->records[serialize($pk)]=$baseRecord;
}
foreach($this->stats as $stat)
$stat->query();
switch(count($this->children))
{
case 0:
return;
break;
case 1:
$child=reset($this->children);
break;
default: // bridge(s) inside
$child=end($this->children);
break;
}
$query=new CJoinQuery($child);
$query->selects=array();
$query->selects[]=$child->getColumnSelect($child->relation->select);
$query->conditions=array();
$query->conditions[]=$child->relation->condition;
$query->conditions[]=$child->relation->on;
$query->groups[]=$child->relation->group;
$query->joins[]=$child->relation->join;
$query->havings[]=$child->relation->having;
$query->orders[]=$child->relation->order;
if(is_array($child->relation->params))
$query->params=$child->relation->params;
$query->elements[$child->id]=true;
if($child->relation instanceof CHasManyRelation)
{
$query->limit=$child->relation->limit;
$query->offset=$child->relation->offset;
}
$child->beforeFind();
$child->applyLazyCondition($query,$baseRecord);
$this->_joined=true;
$child->_joined=true;
$this->_finder->baseLimited=false;
$child->buildQuery($query);
$child->runQuery($query);
foreach($child->children as $c)
$c->find();
if(empty($child->records))
return;
if($child->relation instanceof CHasOneRelation || $child->relation instanceof CBelongsToRelation)
$baseRecord->addRelatedRecord($child->relation->name,reset($child->records),false);
else // has_many and many_many
{
foreach($child->records as $record)
{
if($child->relation->index!==null)
$index=$record->{$child->relation->index};
else
$index=true;
$baseRecord->addRelatedRecord($child->relation->name,$record,$index);
}
}
}
鏍规嵁鎸囧畾鐨勫熀璁板綍鏉ユ墽琛屽欢杩熸煡鎵俱
runQuery()
鏂规硶
public void runQuery(CJoinQuery $query)
| ||
$query | CJoinQuery | 瑕佽鎵ц鐨勬煡璇€ |
婧愮爜锛 framework/db/ar/CActiveFinder.php#815 (鏄剧ず)
public function runQuery($query)
{
$command=$query->createCommand($this->_builder);
foreach($command->queryAll() as $row)
$this->populateRecord($query,$row);
}
鎵ц鍏宠仈鏌ヨ骞跺~鍏呮煡璇㈢粨鏋溿