Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix fieldlist, refs PR808 and PR851

Squashed commit of the following:

commit 7007dba
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 3ca4d23
Author: Ceeram <c33ram@gmail.com>
Date:   Sun Sep 23 12:45:00 2012 +0200

    more tests added

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

    change methodname and visbilty

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

    fix fieldlist, refs PR #808

commit e9db96b
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 adb8142
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.
  • Loading branch information...
commit b8607ca2eda9895242fc892db84f45be9bab94ca 1 parent 7d84486
@ceeram ceeram authored
Showing with 69 additions and 10 deletions.
  1. +26 −0 lib/Cake/Model/Model.php
  2. +43 −10 lib/Cake/Test/Case/Model/ModelWriteTest.php
View
26 lib/Cake/Model/Model.php
@@ -2226,6 +2226,7 @@ public function saveAssociated($data = null, $options = array()) {
} 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 @@ public function saveAssociated($data = null, $options = array()) {
$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 @@ public function saveAssociated($data = null, $options = array()) {
$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;
@@ -2309,6 +2312,29 @@ public function saveAssociated($data = null, $options = array()) {
}
/**
+ * 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.
*
* #### Options
View
53 lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -6470,10 +6470,10 @@ public function testSaveAllFieldListValidateBelongsTo() {
// 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 @@ public function testSaveAllFieldListValidateBelongsTo() {
'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 @@ public function testSaveAllFieldListHasMany() {
$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 @@ public function testSaveAllFieldListHasOne() {
));
$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']);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.