Merge pull request #210 from basuke/1.3

1.3 saveAll() with validation option "only" or "first" works well with associated data.
This commit is contained in:
ADmad 2011-09-23 04:36:45 -07:00
commit 9220837908
2 changed files with 51 additions and 3 deletions

View file

@ -1687,7 +1687,10 @@ class Model extends Overloadable {
$type = $associations[$association];
switch ($type) {
case 'hasOne':
if (!$validating) {
$values[$this->{$type}[$association]['foreignKey']] = $this->id;
}
if (!$this->{$association}->__save($values, $options)) {
$validationErrors[$association] = $this->{$association}->validationErrors;
$validates = false;
@ -1697,9 +1700,12 @@ class Model extends Overloadable {
}
break;
case 'hasMany':
if (!$validating) {
foreach ($values as $i => $value) {
$values[$i][$this->{$type}[$association]['foreignKey']] = $this->id;
}
}
$_options = array_merge($options, array('atomic' => false));
if ($_options['validate'] === 'first') {

View file

@ -643,6 +643,48 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEqual($joinRecords, 0, 'Records were saved on the join table. %s');
}
/**
* test that saveAll and with models at initial insert (no id has set yet)
* with validation interact well
*
* @return void
*/
function testValidatesWithModelsAndSaveAllWithoutId() {
$data = array(
'Article' => array(
'title' => 'Extra Fields',
'body' => 'Extra Fields Body',
'published' => '1'
),
'Comment' => array(
array('word' => 'Hello'),
array('word' => 'World'),
)
);
$Article =& new Article();
$Comment =& $Article->Comment;
$Comment->validate = array('article_id' => array('rule' => 'numeric'));
$Article->create();
$result = $Article->saveAll($data, array('validate' => 'only'));
$this->assertTrue($result);
$Article->create();
$result = $Article->saveAll($data, array('validate' => 'first'));
$this->assertTrue($result);
$this->assertFalse(is_null($Article->id));
$id = $Article->id;
$count = $Article->find('count', array('conditions' => array('Article.id' => $id)));
$this->assertIdentical($count, 1);
$count = $Comment->find('count', array(
'conditions' => array('Comment.article_id' => $id)
));
$this->assertEqual($count, count($data['Comment']));
}
/**
* Test that missing validation methods trigger errors in development mode.
* Helps to make developement easier.