Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix fieldlist, refs PR #808

  • Loading branch information...
commit 70396265025190ab2fbc159911ec3e064cc44fb2 1 parent e9db96b
@ceeram ceeram authored
View
30 lib/Cake/Model/Model.php
@@ -2216,6 +2216,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->addToFieldlist($key, $options);
} else {
$validationErrors[$association] = $this->{$association}->validationErrors;
}
@@ -2246,6 +2247,7 @@ public function saveAssociated($data = null, $options = array()) {
$validates = $this->{$association}->create(null) !== null;
$saved = false;
if ($validates) {
+ $options = $this->{$association}->addToFieldlist($key, $options);
if ($options['deep']) {
$saved = $this->{$association}->saveAssociated($values, array_merge($options, array('atomic' => false)));
} else {
@@ -2266,10 +2268,7 @@ public function saveAssociated($data = null, $options = array()) {
$values[$i] = array_merge(array($key => $this->id), $value, array($key => $this->id));
}
}
- // add foreignKey to whitelist if not already there
- if (!in_array($key, $this->{$association}->whitelist)) {
- $this->{$association}->whitelist[] = $key;
- }
+ $options = $this->{$association}->addToFieldlist($key, $options);
$_return = $this->{$association}->saveMany($values, array_merge($options, array('atomic' => false)));
if (in_array(false, $_return, true)) {
$validationErrors[$association] = $this->{$association}->validationErrors;
@@ -2303,6 +2302,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
+ */
+ public function addToFieldList($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
34 lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -6498,22 +6498,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']));
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.