From a4239efa7cac31d8dbcc83d0c08778a10e69604b Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 18 Jul 2013 08:05:09 -0400 Subject: [PATCH] Fixes #637. --- framework/yii/db/ActiveRecord.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 0b413fe..3ad5bd3 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -1256,6 +1256,9 @@ class ActiveRecord extends Model $relation = $this->getRelation($name); if ($relation->via !== null) { + if ($this->getIsNewRecord() || $model->getIsNewRecord()) { + throw new InvalidCallException('Unable to link models: both models must NOT be newly created.'); + } if (is_array($relation->via)) { /** @var $viaRelation ActiveRelation */ list($viaName, $viaRelation) = $relation->via;