多层Relation 扩展(3.13)

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