fix fieldlist, refs PR808 and PR851

Squashed commit of the following:

commit 7007dba0eb836f852aaca95fada103bc4ba993a9
Merge: 3ca4d23 7d84486
Author: Ceeram <c33ram@gmail.com>
Date:   Fri Nov 2 10:53:39 2012 +0100

    Merge branch '2.3' into calinseciu-2.3

commit 3ca4d231bf80d2bb1c0572196f633da68c1a0db7
Author: Ceeram <c33ram@gmail.com>
Date:   Sun Sep 23 12:45:00 2012 +0200

    more tests added

commit e6b12532655671aff2b966a9be89b2625a715e8f
Author: Ceeram <c33ram@gmail.com>
Date:   Tue Sep 18 09:18:32 2012 +0200

    change methodname and visbilty

commit 70396265025190ab2fbc159911ec3e064cc44fb2
Author: Ceeram <c33ram@gmail.com>
Date:   Thu Sep 6 17:26:52 2012 +0200

    fix fieldlist, refs PR #808

commit e9db96bfe163609eeefba430d8353c822aabacb0
Merge: 99b798f adb8142
Author: Ceeram <c33ram@gmail.com>
Date:   Thu Sep 6 16:26:40 2012 +0200

    Merge branch '2.3' of git://github.com/calinseciu/cakephp into calinseciu-2.3

commit adb8142d2694692ec71a063ac2ad1b275f3a67c9
Author: calinseciu <calinseciu@gmail.com>
Date:   Thu Aug 30 17:57:50 2012 +0300

    Add foreignKey to whitelist in saveAssociated()

    Association's foreignKey doesn't get saved when saving hasMany associations since it's not in the model's whitelist after validation.
    This happens if you don't send the foreignKey with the associated records data.
This commit is contained in:
Ceeram 2012-11-02 10:56:49 +01:00
parent 7d844866f0
commit b8607ca2ed
2 changed files with 69 additions and 10 deletions

View file

@ -2226,6 +2226,7 @@ class Model extends Object implements CakeEventListener {
} else {
$data = array_merge(array($key => $this->{$association}->id), $data, array($key => $this->{$association}->id));
}
$options = $this->_addToWhiteList($key, $options);
} else {
$validationErrors[$association] = $this->{$association}->validationErrors;
}
@ -2256,6 +2257,7 @@ class Model extends Object implements CakeEventListener {
$validates = $this->{$association}->create(null) !== null;
$saved = false;
if ($validates) {
$options = $this->{$association}->_addToWhiteList($key, $options);
if ($options['deep']) {
$saved = $this->{$association}->saveAssociated($values, array_merge($options, array('atomic' => false)));
} else {
@ -2276,6 +2278,7 @@ class Model extends Object implements CakeEventListener {
$values[$i] = array_merge(array($key => $this->id), $value, array($key => $this->id));
}
}
$options = $this->{$association}->_addToWhiteList($key, $options);
$_return = $this->{$association}->saveMany($values, array_merge($options, array('atomic' => false)));
if (in_array(false, $_return, true)) {
$validationErrors[$association] = $this->{$association}->validationErrors;
@ -2308,6 +2311,29 @@ class Model extends Object implements CakeEventListener {
return false;
}
/**
* Helper method for saveAll() and friends, to add foreign key to fieldlist
*
* @param string $key fieldname to be added to list
* @param array $options
* @return array $options
*/
protected function _addToWhiteList($key, $options) {
if (empty($options['fieldList']) && $this->whitelist && !in_array($key, $this->whitelist)) {
$options['fieldList'][$this->alias] = $this->whitelist;
$options['fieldList'][$this->alias][] = $key;
return $options;
}
if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) {
$options['fieldList'][$this->alias][] = $key;
return $options;
}
if (!empty($options['fieldList']) && is_array($options['fieldList'])) {
$options['fieldList'][] = $key;
}
return $options;
}
/**
* Validates a single record, as well as all its directly associated records.
*

View file

@ -6470,10 +6470,10 @@ class ModelWriteTest extends BaseModelTest {
// test belongsTo
$fieldList = array(
'Post' => array('title', 'author_id'),
'Post' => array('title'),
'Author' => array('user')
);
$TestModel->saveAll(array(
$data = array(
'Post' => array(
'title' => 'Post without body',
'body' => 'This will not be saved',
@ -6482,7 +6482,8 @@ class ModelWriteTest extends BaseModelTest {
'user' => 'bob',
'test' => 'This will not be saved',
)), array('fieldList' => $fieldList));
));
$TestModel->saveAll($data, array('fieldList' => $fieldList));
$result = $TestModel->find('all');
$expected = array(
@ -6569,22 +6570,42 @@ class ModelWriteTest extends BaseModelTest {
$this->db->truncate($TestModel);
$this->db->truncate(new Comment());
$fieldList = array(
'Article' => array('id'),
'Comment' => array('article_id', 'user_id')
);
$result = $TestModel->saveAll(array(
'Article' => array('id' => 2, 'title' => 'I will not save'),
$data = array(
'Article' => array('title' => 'I will not save'),
'Comment' => array(
array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1),
array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
)
), array('fieldList' => $fieldList));
);
$fieldList = array(
'Article' => array('id'),
'Comment' => array('article_id', 'user_id')
);
$TestModel->saveAll($data, array('fieldList' => $fieldList));
$result = $TestModel->find('all');
$this->assertEquals('', $result[0]['Article']['title']);
$this->assertEquals('', $result[0]['Comment'][0]['comment']);
$this->assertEquals('', $result[0]['Comment'][1]['comment']);
$fieldList = array(
'Article' => array('id'),
'Comment' => array('user_id')
);
$TestModel->saveAll($data, array('fieldList' => $fieldList));
$result = $TestModel->find('all');
$this->assertEquals('', $result[1]['Article']['title']);
$this->assertEquals(2, count($result[1]['Comment']));
$TestModel->whitelist = array('id');
$TestModel->Comment->whitelist = array('user_id');
$TestModel->saveAll($data);
$result = $TestModel->find('all');
$this->assertEquals('', $result[2]['Article']['title']);
$this->assertEquals(2, count($result[2]['Comment']));
}
/**
@ -6621,6 +6642,18 @@ class ModelWriteTest extends BaseModelTest {
));
$this->assertTrue($result);
$this->assertEmpty($TestModel->validationErrors);
$TestModel->Attachment->whitelist = array('id');
$fieldList = array(
'Comment' => array('id', 'article_id', 'user_id'),
'Attachment' => array('id')
);
$result = $TestModel->saveAll($record, array(
'fieldList' => $fieldList
));
$this->assertTrue($result);
$result = $TestModel->find('first', array('order' => array('Comment.created' => 'DESC')));
$this->assertEquals($result['Comment']['id'], $result['Attachment']['comment_id']);
}
/**