From eef87ee74c234155e21f5b863d3be5e189fef3fa Mon Sep 17 00:00:00 2001 From: Yosuke Basuke Suzuki Date: Fri, 23 Sep 2011 14:40:16 +0900 Subject: [PATCH 1/2] Bug fixed. saveAll with validation option "only" or "first" works wll. saveAll() did set null foreign key when it just validates. I've assigned numeric validation on the hasMany side model and validation did fail with this behavior. I've changed this not to set foreign key when it just validation. --- cake/libs/model/model.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index f595b7529..3d7d454a8 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1687,7 +1687,10 @@ class Model extends Overloadable { $type = $associations[$association]; switch ($type) { case 'hasOne': - $values[$this->{$type}[$association]['foreignKey']] = $this->id; + if (!$validating) { + $values[$this->{$type}[$association]['foreignKey']] = $this->id; + } + if (!$this->{$association}->__save($values, $options)) { $validationErrors[$association] = $this->{$association}->validationErrors; $validates = false; @@ -1697,9 +1700,12 @@ class Model extends Overloadable { } break; case 'hasMany': - foreach ($values as $i => $value) { - $values[$i][$this->{$type}[$association]['foreignKey']] = $this->id; + if (!$validating) { + foreach ($values as $i => $value) { + $values[$i][$this->{$type}[$association]['foreignKey']] = $this->id; + } } + $_options = array_merge($options, array('atomic' => false)); if ($_options['validate'] === 'first') { From d9c48b0d34fcb6e00892683b6bac134d87f885ec Mon Sep 17 00:00:00 2001 From: Yosuke Basuke Suzuki Date: Fri, 23 Sep 2011 18:23:50 +0900 Subject: [PATCH 2/2] Added unittest for eef87ee74c2. --- .../libs/model/model_validation.test.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cake/tests/cases/libs/model/model_validation.test.php b/cake/tests/cases/libs/model/model_validation.test.php index 6688186c1..2e2979b3a 100644 --- a/cake/tests/cases/libs/model/model_validation.test.php +++ b/cake/tests/cases/libs/model/model_validation.test.php @@ -643,6 +643,48 @@ class ModelValidationTest extends BaseModelTest { $this->assertEqual($joinRecords, 0, 'Records were saved on the join table. %s'); } +/** + * test that saveAll and with models at initial insert (no id has set yet) + * with validation interact well + * + * @return void + */ + function testValidatesWithModelsAndSaveAllWithoutId() { + $data = array( + 'Article' => array( + 'title' => 'Extra Fields', + 'body' => 'Extra Fields Body', + 'published' => '1' + ), + 'Comment' => array( + array('word' => 'Hello'), + array('word' => 'World'), + ) + ); + $Article =& new Article(); + $Comment =& $Article->Comment; + + $Comment->validate = array('article_id' => array('rule' => 'numeric')); + + $Article->create(); + $result = $Article->saveAll($data, array('validate' => 'only')); + $this->assertTrue($result); + + $Article->create(); + $result = $Article->saveAll($data, array('validate' => 'first')); + $this->assertTrue($result); + $this->assertFalse(is_null($Article->id)); + + $id = $Article->id; + $count = $Article->find('count', array('conditions' => array('Article.id' => $id))); + $this->assertIdentical($count, 1); + + $count = $Comment->find('count', array( + 'conditions' => array('Comment.article_id' => $id) + )); + $this->assertEqual($count, count($data['Comment'])); + } + /** * Test that missing validation methods trigger errors in development mode. * Helps to make developement easier.