Browse files

make sure model data is moved to alias, even when first key in data i…

…s assoc model

add extra tests for saveAll and validate first
ensure db values dont get reset to default values
  • Loading branch information...
1 parent 7107cd6 commit 7dbd6bc3a2bb17501fb411c6a0605e3908a9d0c5 @ceeram ceeram committed with lorenzo May 15, 2012
View
38 lib/Cake/Model/Model.php
@@ -1129,9 +1129,7 @@ public function set($one, $two = null) {
if (is_array($one)) {
$data = $one;
if (empty($one[$this->alias])) {
- if ($this->getAssociated(key($one)) === null) {
- $data = array($this->alias => $one);
- }
+ $data = $this->_setAliasData($one);
}
} else {
$data = array($this->alias => array($one => $two));
@@ -1161,6 +1159,24 @@ public function set($one, $two = null) {
}
/**
+ * Move values to alias
+ *
+ * @param array $data
+ * @return array
+ */
+ protected function _setAliasData($data) {
+ $models = array_keys($this->getAssociated());
+ $schema = array_keys($this->schema());
+ foreach ($data as $field => $value) {
+ if (in_array($field, $schema) || !in_array($field, $models)) {
+ $data[$this->alias][$field] = $value;
+ unset($data[$field]);
+ }
+ }
+ return $data;
+ }
+
+/**
* Normalize Xml::toArray() to use in Model::save()
*
* @param array $xml XML as array
@@ -2118,7 +2134,8 @@ public function validateMany(&$data, $options = array()) {
if ($options['deep']) {
$validates = $this->validateAssociated($record, $options);
} else {
- $validates = $this->create($record) && $this->validates($options);
+ $this->create(null);
+ $validates = $this->set($record) && $this->validates($options);
$data[$key] = $this->data;
}
if ($validates === false || (is_array($validates) && in_array(false, $validates, true))) {
@@ -2318,21 +2335,14 @@ public function saveAssociated($data = null, $options = array()) {
public function validateAssociated(&$data, $options = array()) {
$options = array_merge(array('atomic' => true, 'deep' => false), $options);
$this->validationErrors = $validationErrors = $return = array();
- if (!($this->create($data) && $this->validates($options))) {
+ $this->create(null);
+ if (!($this->set($data) && $this->validates($options))) {
$validationErrors[$this->alias] = $this->validationErrors;
$return[$this->alias] = false;
} else {
$return[$this->alias] = true;
}
-
- if (empty($options['deep'])) {
- $data = $this->data;
- } else {
- $modelData = $this->data;
- $recordData = $modelData[$this->alias];
- unset($modelData[$this->alias]);
- $data = $modelData + array_merge($data, $recordData);
- }
+ $data = $this->data;
$associations = $this->getAssociated();
foreach ($data as $association => &$values) {
View
2 lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -7868,7 +7868,7 @@ public function testfindCustom() {
$Article = new CustomArticle();
$data = array('user_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
$Article->create($data);
- $Article->save();
+ $Article->save(null, false);
$this->assertEquals(4, $Article->id);
$result = $Article->find('published');
View
67 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -1016,22 +1016,34 @@ public function testValidateFirstWithBeforeValidate() {
);
$result = $model->saveAll($data, array('validate' => 'first'));
$this->assertTrue($result);
+ $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
- $title = $model->field('title', array('body' => 'foo0'));
+ $model->findMethods['unPublished'] = true;
+ $data = array(
+ 'CustomArticle' => array(
+ 'body' => 'foo1'
+ )
+ );
+ $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
+ $this->assertTrue($result);
+ $title = $model->field('title', array('body' => 'foo1'));
$this->assertEquals('foo', $title);
+ $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
$data = array(
- array('body' => 'foo1'),
array('body' => 'foo2'),
- array('body' => 'foo3')
+ array('body' => 'foo3'),
+ array('body' => 'foo4')
);
$result = $model->saveAll($data, array('validate' => 'first'));
$this->assertTrue($result);
+ $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
+ $this->assertTrue($result);
- $this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
$this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
$this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
+ $this->assertEquals('foo', $model->field('title', array('body' => 'foo4')));
}
/**
@@ -1040,7 +1052,7 @@ public function testValidateFirstWithBeforeValidate() {
*
* @return void
*/
- public function testValidateAssociatedWithBeforeValidate() {
+ public function testValidateFirstAssociatedWithBeforeValidate() {
$this->loadFixtures('Article', 'User');
$model = new CustomArticle();
$model->validate = array(
@@ -1070,4 +1082,49 @@ public function testValidateAssociatedWithBeforeValidate() {
$this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
}
+/**
+ * testValidateFirstWithDefaults method
+ *
+ * return @void
+ */
+ public function testFirstWithDefaults() {
+ $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
+ $TestModel = new Article();
+
+ $result = $TestModel->find('first', array(
+ 'conditions' => array('Article.id' => 1)
+ ));
+ $expected = array(
+ 'Article' => array(
+ 'id' => 1,
+ 'user_id' => 1,
+ 'title' => 'First Article',
+ 'body' => 'First Article Body',
+ 'published' => 'Y',
+ 'created' => '2007-03-18 10:39:23'
+ ),
+ );
+ unset($result['Article']['updated']);
+ $this->assertEquals($expected['Article'], $result['Article']);
+
+ $data = array(
+ 'Article' => array(
+ 'id' => 1,
+ 'title' => 'First Article (modified)'
+ ),
+ 'Comment' => array(
+ array('comment' => 'Article comment', 'user_id' => 1)
+ )
+ );
+ $result = $TestModel->saveAll($data, array('validate' => 'first'));
+ $this->assertTrue($result);
+
+ $result = $TestModel->find('first', array(
+ 'conditions' => array('Article.id' => 1)
+ ));
+ $expected['Article']['title'] = 'First Article (modified)';
+ unset($result['Article']['updated']);
+ $this->assertEquals($expected['Article'], $result['Article']);
+ }
+
}
View
10 lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -4277,7 +4277,7 @@ public function testSaveAllValidation() {
'author_id' => '3',
'title' => 'Just update the title',
'body' => 'Second Post Body',
- 'published' => 'N',
+ 'published' => 'Y',
'created' => '2007-03-18 10:41:23'
)),
array(
@@ -4366,7 +4366,7 @@ public function testSaveAllValidation() {
'author_id' => '3',
'title' => 'Just update the title',
'body' => 'Second Post Body',
- 'published' => 'N',
+ 'published' => 'Y',
'created' => '2007-03-18 10:41:23'
)
),
@@ -4734,7 +4734,7 @@ public function testSaveAssociated() {
'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
)));
- $result = $TestModel->find('all');
+ $result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
$expected = array(
'Post' => array(
'id' => '4',
@@ -5631,7 +5631,7 @@ public function testSaveManyValidation() {
'author_id' => '3',
'title' => 'Just update the title',
'body' => 'Second Post Body',
- 'published' => 'N',
+ 'published' => 'Y',
'created' => '2007-03-18 10:41:23'
)
),
@@ -5727,7 +5727,7 @@ public function testSaveManyValidation() {
'author_id' => '3',
'title' => 'Just update the title',
'body' => 'Second Post Body',
- 'published' => 'N',
+ 'published' => 'Y',
)),
array(
'Post' => array(
View
7 lib/Cake/Test/Case/Model/models.php
@@ -3119,7 +3119,7 @@ class TranslatedItem2 extends CakeTestModel {
/**
* translateModel property
*
- * @var string
+ * @var string
*/
public $translateModel = 'TranslateWithPrefix';
@@ -4975,6 +4975,11 @@ protected function _findUnPublished($state, $query, $results = array()) {
**/
public function beforeValidate($options = array()) {
$this->data[$this->alias]['title'] = 'foo';
+ if ($this->findMethods['unPublished'] === true) {
+ $this->findMethods['unPublished'] = false;
+ } else {
+ $this->findMethods['unPublished'] = 'true again';
+ }
}
}

0 comments on commit 7dbd6bc

Please sign in to comment.