重载或重写 getRelation函数,以获得多层关联,关联参数:array('mapping_name'=>'next_mappingname_or_array', 'normal_mapping_name') /**<br>
* 获取返回数据的关联记录<br>
* @access protected<br>
* @param mixed $result 返回数据<br>
* @param string|array $name 关联名称<br>
* @param boolean $return 是否返回关联数据本身<br>
* @return array<br>
*/<br>
protected function getRelation(&$result,$name='',$return=false) {<br>
if(!empty($this->_link)) {<br>
foreach($this->_link as $key=>$val) {<br>
$mappingName = !empty($val['mapping_name'])?$val['mapping_name']:$key; // 映射名称<br>
if(empty($name) || true === $name || $mappingName == $name || (is_array($name) && (in_array($mappingName,$name) || array_key_exists($mappingName,$name) ))) {<br>
$mappingType = !empty($val['mapping_type'])?$val['mapping_type']:$val; // 关联类型<br>
$mappingClass = !empty($val['class_name'])?$val['class_name']:$key; // 关联类名<br>
$mappingFields = !empty($val['mapping_fields'])?$val['mapping_fields']:'*'; // 映射字段<br>
$mappingCondition = !empty($val['condition'])?$val['condition']:'1=1'; // 关联条件<br>
$mappingKey =!empty($val['mapping_key'])? $val['mapping_key'] : $this->getPk(); // 关联键名<br>
if(strtoupper($mappingClass)==strtoupper($this->name)) {<br>
// 自引用关联 获取父键名<br>
$mappingFk = !empty($val['parent_key'])? $val['parent_key'] : 'parent_id';<br>
}else{<br>
$mappingFk = !empty($val['foreign_key'])?$val['foreign_key']:strtolower($this->name).'_id'; // 关联外键<br>
}<br>
// 获取关联模型对象<br>
$model = D($mappingClass);<br>
switch($mappingType) {<br>
case HAS_ONE:<br>
$pk = $result[$mappingKey];<br>
$mappingCondition .= " AND {$mappingFk}='{$pk}'";<br>
if (is_array($name) && array_key_exists($mappingName,$name)){<br>
$relationData = $model->relation($name[$mappingName])->where($mappingCondition)->field($mappingFields)->find();<br>
}else{<br>
$relationData = $model->where($mappingCondition)->field($mappingFields)->find();<br>
}<br>
break;<br>
case BELONGS_TO:<br>
if(strtoupper($mappingClass)==strtoupper($this->name)) {<br>
// 自引用关联 获取父键名<br>
$mappingFk = !empty($val['parent_key'])? $val['parent_key'] : 'parent_id';<br>
}else{<br>
$mappingFk = !empty($val['foreign_key'])?$val['foreign_key']:strtolower($model->getModelName()).'_id'; // 关联外键<br>
}<br>
$fk = $result[$mappingFk];<br>
$mappingCondition .= " AND {$model->getPk()}='{$fk}'";<br>
if (is_array($name) && array_key_exists($mappingName,$name)){<br>
$relationData = $model->relation($name[$mappingName])->where($mappingCondition)->field($mappingFields)->find();<br>
}else<br>
$relationData = $model->where($mappingCondition)->field($mappingFields)->find();<br>
break;<br>
case HAS_MANY:<br>
$pk = $result[$mappingKey];<br>
$mappingCondition .= " AND {$mappingFk}='{$pk}'";<br>
$mappingOrder = !empty($val['mapping_order'])?$val['mapping_order']:'';<br>
$mappingLimit = !empty($val['mapping_limit'])?$val['mapping_limit']:'';<br>
// 延时获取关联记录<br>
if (is_array($name) && array_key_exists($mappingName,$name)){<br>
$relationData = $model->relation($name[$mappingName])->where($mappingCondition)->field($mappingFields)->order($mappingOrder)->limit($mappingLimit)->select();<br>
}else<br>
$relationData = $model->where($mappingCondition)->field($mappingFields)->order($mappingOrder)->limit($mappingLimit)->select();<br>
break;<br>
case MANY_TO_MANY:<br>
$pk = $result[$mappingKey];<br>
$mappingCondition = " {$mappingFk}='{$pk}'";<br>
$mappingOrder = $val['mapping_order'];<br>
$mappingLimit = $val['mapping_limit'];<br>
$mappingRelationFk = $val['relation_foreign_key']?$val['relation_foreign_key']:$model->getModelName().'_id';<br>
$mappingRelationTable = $val['relation_table']?$val['relation_table']:$this->getRelationTableName($model);<br>
$sql = "SELECT b.{$mappingFields} FROM {$mappingRelationTable} AS a, ".$model->getTableName()." AS b WHERE a.{$mappingRelationFk} = b.{$model->getPk()} AND a.{$mappingCondition}";<br>
if(!empty($val['condition'])) {<br>
$sql .= ' AND '.$val['condition'];<br>
}<br>
if(!empty($mappingOrder)) {<br>
$sql .= ' ORDER BY '.$mappingOrder;<br>
}<br>
if(!empty($mappingLimit)) {<br>
$sql .= ' LIMIT '.$mappingLimit;<br>
}<br>
if (is_array($name) && array_key_exists($mappingName,$name)){ <br>
<br>
$mappingOrder = !empty($val['mapping_order'])?$val['mapping_order']:'';<br>
$mappingLimit = !empty($val['mapping_limit'])?$val['mapping_limit']:'';<br>
$relationData = $model->relation($name[$mappingName])<br>
->field($model->getTableName().'.'.$mappingFields)<br>
->join($mappingRelationTable." on $mappingRelationTable.$mappingRelationFk = ".$model->getTableName().".".$model->getPk())<br>
->where($mappingRelationTable.".{$mappingFk}='{$pk}'")<br>
->order($mappingOrder)<br>
->limit($mappingLimit)<br>
->select();<br>
}else<br>
$relationData = $this->query($sql);<br>
break;<br>
}<br>
if(!$return){<br>
if(isset($val['as_fields']) && in_array($mappingType,array(HAS_ONE,BELONGS_TO)) ) {<br>
// 支持直接把关联的字段值映射成数据对象中的某个字段<br>
// 仅仅支持HAS_ONE BELONGS_TO<br>
$fields = explode(',',$val['as_fields']);<br>
foreach ($fields as $field){<br>
if(strpos($field,':')) {<br>
list($relationName,$nick) = explode(':',$field);<br>
$result[$nick] = $relationData[$relationName];<br>
}else{<br>
$result[$field] = $relationData[$field];<br>
}<br>
}<br>
}else{<br>
$result[$mappingName] = $relationData;<br>
}<br>
unset($relationData);<br>
}else{<br>
return $relationData;<br>
}<br>
}<br>
}<br>
}<br>
return $result;<br>
}<br>
AD:真正免费,域名+虚机+企业邮箱=0元