Permalink
Browse files

fix error in previous commit

  • Loading branch information...
1 parent a845977 commit 388b20dd9d4555c60aaddafb18f87e50c3d17563 @ceeram ceeram committed Apr 16, 2012
Showing with 22 additions and 3 deletions.
  1. +4 −2 lib/Cake/Model/Model.php
  2. +18 −1 lib/Cake/Test/Case/Model/ModelWriteTest.php
View
@@ -2191,7 +2191,8 @@ public function saveAssociated($data = null, $options = array()) {
$return = array();
$validates = true;
foreach ($data as $association => $values) {
- if (isset($associations[$association]) && $associations[$association] === 'belongsTo') {
+ $notEmpty = !empty($values[$association]) || (!isset($values[$association]) && !empty($values));
+ if (isset($associations[$association]) && $associations[$association] === 'belongsTo' && $notEmpty) {
$validates = $this->{$association}->create(null) !== null;
$saved = false;
if ($validates) {
@@ -2225,7 +2226,8 @@ public function saveAssociated($data = null, $options = array()) {
if (!$validates) {
break;
}
- if (isset($associations[$association])) {
+ $notEmpty = !empty($values[$association]) || (!isset($values[$association]) && !empty($values));
+ if (isset($associations[$association]) && $notEmpty) {
$type = $associations[$association];
$key = $this->{$type}[$association]['foreignKey'];
switch ($type) {
@@ -5237,11 +5237,28 @@ public function testSaveAssociatedHasManyEmpty() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
+ $TestModel->validate = $TestModel->Comment->validate = array('user_id' => array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
+
+ //empty hasMany data is ignored in save
$result = $TestModel->saveAssociated(array(
- 'Article' => array('title' => 'title', 'author_id' => 1),
+ 'Article' => array('title' => 'title', 'user_id' => 1),
'Comment' => array()
), array('validate' => true));
$this->assertTrue($result);
+
+ $result = $TestModel->saveAssociated(array(
+ 'Article' => array('title' => 'title', 'user_id' => 1),
+ 'Comment' => array()
+ ), array('validate' => true, 'atomic' => false));
+ $this->assertEquals(array('Article' => true), $result);
+
+ //empty primary data is not ignored
+ $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true));
+ $this->assertFalse($result);
+
+ $result = $TestModel->saveAssociated(array('Article' => array()), array('validate' => true, 'atomic' => false));
+ $this->assertEquals(array('Article' => false), $result);
+
}
/**

0 comments on commit 388b20d

Please sign in to comment.