Skip to content

Commit

Permalink
fix error in previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Apr 16, 2012
1 parent a845977 commit 388b20d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/Cake/Model/Model.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 18 additions & 1 deletion lib/Cake/Test/Case/Model/ModelWriteTest.php
Expand Up @@ -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);

}

/**
Expand Down

0 comments on commit 388b20d

Please sign in to comment.