From b85c87bc55b4da839cd7a4679eec2a67720c8041 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sat, 25 May 2013 01:05:51 +0200 Subject: [PATCH] AR relations copy&paste --- framework/yii/db/redis/ActiveRecord.php | 64 ++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/framework/yii/db/redis/ActiveRecord.php b/framework/yii/db/redis/ActiveRecord.php index 4c495e1..31e8171 100644 --- a/framework/yii/db/redis/ActiveRecord.php +++ b/framework/yii/db/redis/ActiveRecord.php @@ -10,6 +10,7 @@ namespace yii\db\redis; +use yii\base\InvalidCallException; use yii\base\InvalidConfigException; use yii\base\InvalidParamException; use yii\base\NotSupportedException; @@ -420,34 +421,12 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord */ public function link($name, $model, $extraColumns = array()) { - // TODO $relation = $this->getRelation($name); if ($relation->via !== null) { - if (is_array($relation->via)) { - /** @var $viaRelation ActiveRelation */ - list($viaName, $viaRelation) = $relation->via; - /** @var $viaClass ActiveRecord */ - $viaClass = $viaRelation->modelClass; - $viaTable = $viaClass::tableName(); - // unset $viaName so that it can be reloaded to reflect the change - unset($this->_related[strtolower($viaName)]); - } else { - $viaRelation = $relation->via; - $viaTable = reset($relation->via->from); - } - $columns = array(); - foreach ($viaRelation->link as $a => $b) { - $columns[$a] = $this->$b; - } - foreach ($relation->link as $a => $b) { - $columns[$b] = $model->$a; - } - foreach ($extraColumns as $k => $v) { - $columns[$k] = $v; - } - static::getDb()->createCommand() - ->insert($viaTable, $columns)->execute(); + // TODO + + } else { $p1 = $model->isPrimaryKey(array_keys($relation->link)); $p2 = $this->isPrimaryKey(array_values($relation->link)); @@ -482,6 +461,24 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord } /** + * @param array $link + * @param ActiveRecord $foreignModel + * @param ActiveRecord $primaryModel + * @throws InvalidCallException + */ + private function bindModels($link, $foreignModel, $primaryModel) + { + foreach ($link as $fk => $pk) { + $value = $primaryModel->$pk; + if ($value === null) { + throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.'); + } + $foreignModel->$fk = $value; + } + $foreignModel->save(false); + } + + /** * Destroys the relationship between two models. * * The model with the foreign key of the relationship will be deleted if `$delete` is true. @@ -558,6 +555,23 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord } } + /** + * TODO duplicate code, refactor + * @param array $keys + * @return boolean + */ + private function isPrimaryKey($keys) + { + $pks = $this->primaryKey(); + foreach ($keys as $key) { + if (!in_array($key, $pks, true)) { + return false; + } + } + return true; + } + + // TODO implement link and unlink }