Commit cf6c73c6 by Qiang Xue

link relation WIP

parent ef8b0922
...@@ -873,6 +873,8 @@ abstract class ActiveRecord extends Model ...@@ -873,6 +873,8 @@ abstract class ActiveRecord extends Model
/** @var $viaClass ActiveRecord */ /** @var $viaClass ActiveRecord */
$viaClass = $viaQuery->modelClass; $viaClass = $viaQuery->modelClass;
$viaTable = $viaClass::tableName(); $viaTable = $viaClass::tableName();
// unset $viaName so that it can be reloaded to reflect the change
unset($this->_related[$viaName]);
} else { } else {
$viaQuery = $relation->via; $viaQuery = $relation->via;
$viaTable = reset($relation->via->from); $viaTable = reset($relation->via->from);
...@@ -889,8 +891,18 @@ abstract class ActiveRecord extends Model ...@@ -889,8 +891,18 @@ abstract class ActiveRecord extends Model
} }
$command = $this->getDbConnection()->createCommand(); $command = $this->getDbConnection()->createCommand();
$command->insert($viaTable, $columns)->execute(); $command->insert($viaTable, $columns)->execute();
$name = strtolower($name);
if (!$relation->multiple) {
$this->_related[$name] = $model;
} elseif (isset($this->_related[$name])) {
if ($relation->indexBy !== null) {
$indexBy = $relation->indexBy;
$this->_related[$name][$model->$indexBy] = $model;
} else {
$this->_related[$name][] = $model;
}
}
return; return;
// todo: update $viaName
} }
$keys = $model->primaryKey(); $keys = $model->primaryKey();
$p1 = true; $p1 = true;
...@@ -953,7 +965,18 @@ abstract class ActiveRecord extends Model ...@@ -953,7 +965,18 @@ abstract class ActiveRecord extends Model
} else { } else {
throw new Exception(''); throw new Exception('');
} }
// todo: update relation models
if (!$relation->multiple) {
$this->_related[$name] = $model;
} elseif (isset($this->_related[$name])) {
if ($relation->indexBy !== null) {
$indexBy = $relation->indexBy;
$this->_related[$name][$model->$indexBy] = $model;
} else {
$this->_related[$name][] = $model;
}
}
return;
} }
/** /**
...@@ -1022,7 +1045,17 @@ abstract class ActiveRecord extends Model ...@@ -1022,7 +1045,17 @@ abstract class ActiveRecord extends Model
} else { } else {
throw new Exception(''); throw new Exception('');
} }
// todo: update relation models
if (!$relation->multiple) {
unset($this->_related[$name]);
} elseif (isset($this->_related[$name])) {
/** @var $b ActiveRecord */
foreach ($this->_related[$name] as $a => $b) {
if ($model->getPrimaryKey() == $b->getPrimaryKey()) {
unset($this->_related[$name][$a]);
}
}
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment