/** * 添加一个对象关联 * * $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; }