/** * Return list of relations for current model * @return array */ public static function getRelations() { $old = parent::getRelations(); unset($old['replies']); $old['parent'] = [DbRelations::BELONGS_TO, '\\mpf\\modules\\forum\\models\\ForumReplySeventh', 'reply_id']; $old['replies'] = DbRelation::hasMany(ForumReplyNth::className())->columnsEqual('id', 'reply_id')->hasValue('level', 9); $old['myVote'] = DbRelation::hasOne(ForumReplyVote::className())->columnsEqual('id', 'reply_id')->hasValue('level', 8)->hasValue('user_id', WebApp::get()->user()->isConnected() ? WebApp::get()->user()->id : 0); return $old; }
/** * @param $path * @param DbRelation $parentRelation * @return DbRelation */ protected function initSubRelation($path, DbRelation $parentRelation) { $modelClass = $parentRelation->model; $relations = $modelClass::getRelations(); $name = explode('.', $path); $name = $name[count($name) - 1]; $relation = $relations[$name]; if (is_array($relation)) { $details = $relation; $relationClass = $details[1]; switch ($details[0]) { case DbRelations::BELONGS_TO: $relation = DbRelation::belongsTo($details[1], $details[2]); break; case DbRelations::HAS_ONE: $relation = DbRelation::hasOne($details[1])->columnsEqual($modelClass::getDb()->getTablePk($modelClass::getTableName()), $details[2]); break; case DbRelations::HAS_MANY: $relation = DbRelation::hasMany($details[1])->columnsEqual($modelClass::getDb()->getTablePk($modelClass::getTableName()), $details[2]); break; case DbRelations::MANY_TO_MANY: //table_name(model_column, relation_column) list($tableName, $columns) = explode("(", $details[2]); $columns = explode(',', substr($columns, 0, strlen($columns) - 1)); $relation = DbRelation::manyToMany($details[1])->join([$tableName => $name . '_' . $tableName], [$modelClass::getDb()->getTablePk($modelClass::getTableName()) => trim($columns[0])])->columnsEqual($name . '_' . $tableName . '.' . trim($columns[1]), $relationClass::getDb()->getTablePk($relationClass::getTableName())); break; default: trigger_error("Invalid relation type {$details[0]}!"); } } return $relation; }