Updating Model::saveAll(), adding tests. Fixes #3871

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6410 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-01-27 01:57:39 +00:00
parent d9691a19c8
commit b9d3c4e2e8
3 changed files with 37 additions and 8 deletions

View file

@ -1578,10 +1578,6 @@ class DboSource extends DataSource {
if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
for ($i = 0; $i < $count; $i++) {
if (!isset($fields[$i])) {
pr("$i not set");
pr($fields);
}
if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
$prepend = '';

View file

@ -1411,8 +1411,19 @@ class Model extends Overloadable {
foreach ($data as $association => $values) {
if (isset($associations[$association])) {
switch ($associations[$association]) {
case 'hasOne':
$type = $associations[$association];
$this->{$association}->set($this->{$type}[$association]['foreignKey'], $this->id);
if (!$result = $this->{$association}->save($values, $options['validate'], $options['fieldList'])) {
$db->rollback($this);
return false;
}
break;
case 'hasMany':
$this->{$association}->saveAll($values);
if (!$this->{$association}->saveAll($values, $options)) {
$db->rollback($this);
return false;
}
break;
}
}

View file

@ -2272,7 +2272,7 @@ class ModelTest extends CakeTestCase {
}
function testSaveAll() {
$this->loadFixtures('Post', 'Author');
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment');
$this->model =& new Post();
$result = $this->model->find('all');
@ -2296,6 +2296,10 @@ class ModelTest extends CakeTestCase {
$this->model->deleteAll(true);
$this->assertEqual($this->model->find('all'), array());
// SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass
$db =& ConnectionManager::getDataSource('test_suite');
$db->truncate($this->model);
$ts = date('Y-m-d H:i:s');
$this->model->saveAll(array(
array('title' => 'Multi-record post 1', 'body' => 'First multi-record post', 'author_id' => 2),
@ -2304,10 +2308,28 @@ class ModelTest extends CakeTestCase {
$result = $this->model->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
$expected = array(
array('Post' => array('id' => '5', 'author_id' => '2', 'title' => 'Multi-record post 1', 'body' => 'First multi-record post', 'published' => 'N', 'created' => $ts, 'updated' => $ts)),
array('Post' => array('id' => '6', 'author_id' => '2', 'title' => 'Multi-record post 2', 'body' => 'Second multi-record post', 'published' => 'N', 'created' => $ts, 'updated' => $ts))
array('Post' => array('id' => '1', 'author_id' => '2', 'title' => 'Multi-record post 1', 'body' => 'First multi-record post', 'published' => 'N', 'created' => $ts, 'updated' => $ts)),
array('Post' => array('id' => '2', 'author_id' => '2', 'title' => 'Multi-record post 2', 'body' => 'Second multi-record post', 'published' => 'N', 'created' => $ts, 'updated' => $ts))
);
$this->assertEqual($result, $expected);
$this->model =& new Comment();
$ts = date('Y-m-d H:i:s');
$result = $this->model->saveAll(array(
'Comment' => array('article_id' => 2, 'user_id' => 2, 'comment' => 'New comment with attachment', 'published' => 'Y'),
'Attachment' => array('attachment' => 'some_file.tgz')
));
$this->assertTrue($result);
$result = $this->model->find('all');
$expected = array('id' => '7', 'article_id' => '2', 'user_id' => '2', 'comment' => 'New comment with attachment', 'published' => 'Y', 'created' => $ts, 'updated' => $ts);
$this->assertEqual($result[6]['Comment'], $expected);
$expected = array('id' => '7', 'article_id' => '2', 'user_id' => '2', 'comment' => 'New comment with attachment', 'published' => 'Y', 'created' => $ts, 'updated' => $ts);
$this->assertEqual($result[6]['Comment'], $expected);
$expected = array('id' => '2', 'comment_id' => '7', 'attachment' => 'some_file.tgz', 'created' => $ts, 'updated' => $ts);
$this->assertEqual($result[6]['Attachment'], $expected);
}
function testSaveWithCounterCache() {