public function getDModel() { if (is_null($this->_dModel)) { $this->_dModel = Leb_Helper::loadModel('document', array(), 'document'); } return $this->_dModel; }
/** * * 得到完整的数据表名 * * @access public * * @return string * */ public function getTableName() { if (empty($this->trueTableName)) { $tableName = ''; $len = 0; foreach ($this->_viewFields as $key => $view) { // 获取数据表名称 $Model = Leb_Helper::loadModel($key, $this->_dbConfig, $this->_application); $tableName .= $Model->getTableName(); // 表别名定义 $tableName .= !empty($view['_as']) ? ' ' . $view['_as'] : ' ' . $key; // 支持ON 条件定义 $tableName .= !empty($view['_on']) ? ' ON ' . $view['_on'] : ''; // 指定JOIN类型 例如 RIGHT INNER LEFT 下一个表有效 $type = !empty($view['_type']) ? $view['_type'] : ''; $tableName .= ' ' . strtoupper($type) . ' JOIN '; $len = strlen($type . '_JOIN '); } $tableName = substr($tableName, 0, -$len); $this->trueTableName = $tableName; } return $this->trueTableName; }
/** * * 操作关联数据 * * @access protected * * @param string $opType 操作方式 ADD SAVE DEL * @param mixed $data 数据对象 * @param string $name 关联名称 * * @return mixed * */ protected function opRelation($opType, $data = '', $name = '') { $result = false; if (empty($data) && !empty($this->data)) { $data = $this->data; } elseif (!is_array($data)) { // 数据无效返回 return false; } if (!empty($this->_link)) { // 遍历关联定义 foreach ($this->_link as $key => $val) { // 操作制定关联类型 $mappingName = $val['mapping_name'] ? $val['mapping_name'] : $key; // 映射名称 if (empty($name) || true === $name || $mappingName == $name || is_array($name) && in_array($mappingName, $name)) { //操作制定的关联 //关联类型 $mappingType = !empty($val['mapping_type']) ? $val['mapping_type'] : $val; //关联类名 $mappingClass = !empty($val['class_name']) ? $val['class_name'] : $key; //当前数据对象主键值 $pk = $data[$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'; } $mappingCondition = !empty($val['condition']) ? $val['condition'] : "{$mappingFk}='{$pk}'"; //获取关联model对象 $model = Leb_Helper::loadModel($mappingClass, $this->_dbConfig, $this->_application); $mappingData = isset($data[$mappingName]) ? $data[$mappingName] : false; if (!empty($mappingData) || $opType == 'DEL') { switch ($mappingType) { case HAS_ONE: switch (strtoupper($opType)) { case 'ADD': //增加关联数据 $mappingData[$mappingFk] = $pk; $result = $model->add($mappingData); break; case 'SAVE': //更新关联数据 $result = $model->where($mappingCondition)->save($mappingData); break; case 'DEL': //根据外键删除关联数据 $result = $model->where($mappingCondition)->delete(); break; } break; case BELONGS_TO: break; case HAS_MANY: switch (strtoupper($opType)) { case 'ADD': //增加关联数据 $model->startTrans(); foreach ($mappingData as $val) { $val[$mappingFk] = $pk; $result = $model->add($val); } $model->commit(); break; case 'SAVE': //更新关联数据 $model->startTrans(); $pk = $model->getPk(); foreach ($mappingData as $vo) { if (isset($vo[$pk])) { //更新数据 $mappingCondition = "{$pk} ={$vo[$pk]}"; $result = $model->where($mappingCondition)->save($vo); } else { // 新增数据 $vo[$mappingFk] = $data[$this->getPk()]; $result = $model->add($vo); } } $model->commit(); break; case 'DEL': //删除关联数据 $result = $model->where($mappingCondition)->delete(); break; } break; case MANY_TO_MANY: //关联 $mappingRelationFk = $val['relation_foreign_key'] ? $val['relation_foreign_key'] : $model->getModelName() . '_id'; $mappingRelationTable = $val['relation_table'] ? $val['relation_table'] : $this->getRelationTableName($model); if (is_array($mappingData)) { $ids = array(); foreach ($mappingData as $vo) { $ids[] = $vo[$model->getPk()]; } $relationId = implode(',', $ids); } switch (strtoupper($opType)) { case 'ADD': //增加关联数据 //增加关联数据 case 'SAVE': //更新关联数据 if (isset($relationId)) { $this->startTrans(); // 删除关联表数据 $this->table($mappingRelationTable)->where($mappingCondition)->delete(); // 插入关联表数据 $sql = 'INSERT INTO ' . $mappingRelationTable . ' (' . $mappingFk . ',' . $mappingRelationFk . ') SELECT a.' . $this->getPk() . ',b.' . $model->getPk() . ' FROM ' . $this->getTableName() . ' AS a ,' . $model->getTableName() . " AS b\n \t\tWHERE a." . $this->getPk() . ' =' . $pk . ' AND b.' . $model->getPk() . ' IN (' . $relationId . ") "; $result = $model->execute($sql); if (false !== $result) { // 提交事务 $this->commit(); } else { // 事务回滚 $this->rollback(); } } break; case 'DEL': // 根据外键删除中间表关联数据 $result = $this->table($mappingRelationTable)->where($mappingCondition)->delete(); break; } break; } } } } } return $result; }
/** * 加载Model * * @param string $modelName * @param array $dbconfig * @param string $application * @return Leb_Model */ public function loadModel($modelName, $dbConfig = array(), $application = '') { return Leb_Helper::loadModel($modelName, $dbConfig, $application, $this->_controller, $this->_action); }
/** * 加载Model * * @param string $modelName * @param array $dbconfig * @param string $application * @return Leb_Model */ public function loadModel($modelName, $dbConfig = array(), $application = '') { return Leb_Helper::loadModel($modelName, $dbConfig, $application); }