function init() { if ($this->_inited) { return $this; } parent::init(); $p = $this->_init_config; $this->source_key = !empty($p['source_key']) ? $p['source_key'] : reset($this->source_meta->idname); $this->target_key = !empty($p['target_key']) ? $p['target_key'] : reset($this->source_meta->idname); unset($this->_init_config); return $this; }
function init() { if ($this->_inited) { return $this; } parent::init(); $p = $this->_init_config; if (empty($p['mid_class'])) { // 如果没有指定中间表对应的 ActiveRecord 类,则使用表数据入口直接处理中间表 $this->mid_meta = null; if (empty($p['mid_table_name'])) { // 尝试自动确定中间表名称 $t1 = $this->source_meta->table->name; $t2 = $this->target_meta->table->name; if ($t1 <= $t2) { $mid_table_name = $t1 . '_has_' . $t2; } else { $mid_table_name = $t2 . '_has_' . $t1; } } else { $mid_table_name = $p['mid_table_name']; } $this->mid_table = new QDB_Table(array('name' => $mid_table_name)); } else { // 如果中间表作为实体,则由指定的 ActiveRecord 继承类负责处理中间表 $this->mid_meta = QDB_ActiveRecord_Meta::instance($p['mid_class']); $this->mid_table = $this->mid_meta->table; } $this->source_key = !empty($p['source_key']) ? $p['source_key'] : reset($this->source_meta->idname); $this->target_key = !empty($p['target_key']) ? $p['target_key'] : reset($this->target_meta->idname); $this->mid_source_key = !empty($p['mid_source_key']) ? $p['mid_source_key'] : reset($this->source_meta->idname); $this->mid_target_key = !empty($p['mid_target_key']) ? $p['mid_target_key'] : reset($this->target_meta->idname); $this->mid_mapping_to = !empty($p['mid_mapping_to']) ? $p['mid_mapping_to'] : 'mid_data'; $class = $this->target_meta->class_name; // $this->source_meta->addDynamicMethod("bind{$class}", array($this, 'bindTarget')); // $this->source_meta->addDynamicMethod("unbind{$class}", array($this, 'unbindTarget')); unset($this->_init_config); return $this; }
/** * 添加一个对象关联 * * $prop_name 参数指定使用 ActiveRecord 对象的什么属性来引用关联的对象。 * 例如“文章”对象的 comments 属性引用了多个关联的“评论”对象。 * * $assoc_type 指定了关联的类型,可以是 QDB::BELONGS_TO、QDB::HAS_MANY、QDB::HAS_ONE 或 QDB::MANY_TO_MANY。 * * $config 指定了关联的属性,可用的属性有多项。 * * @param string $prop_name * @param int $assoc_type * @param array $config * * @return QDB_ActiveRecord_Meta */ function addAssoc($prop_name, $assoc_type, array $config) { switch ($assoc_type) { case QDB::HAS_ONE: case QDB::HAS_MANY: if (empty($config['target_key'])) { $config['target_key'] = strtolower($this->class_name) . '_id'; } break; case QDB::BELONGS_TO: if (empty($config['source_key'])) { $config['source_key'] = strtolower($config['assoc_class']) . '_id'; } break; case QDB::MANY_TO_MANY: if (empty($config['mid_source_key'])) { $config['mid_source_key'] = strtolower($this->class_name) . '_id'; } if (empty($config['mid_target_key'])) { $config['mid_target_key'] = strtolower($config['assoc_class']) . '_id'; } } $assoc = $config['assoc_params']; $assoc['mapping_name'] = $prop_name; $assoc['target_class'] = $config['assoc_class']; unset($assoc[$assoc_type]); $association = QDB_ActiveRecord_Association_Abstract::create($assoc_type, $assoc, $this); $association->registerCallbacks($assoc); $this->associations[$prop_name] = $association; if ($association->type == QDB::BELONGS_TO) { $association->init(); $this->belongsto_props[$association->source_key] = $association; } return $association; }