diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index ecc43dffc..de5001ca8 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1266,6 +1266,7 @@ class Model extends Overloadable { $db =& ConnectionManager::getDataSource($this->useDbConfig); foreach ($joined as $assoc => $value) { + $newValues = array(); if (isset($this->hasAndBelongsToMany[$assoc])) { list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']); $conditions = array($this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id); @@ -1776,7 +1777,7 @@ class Model extends Overloadable { if (empty($results)) { return array(); } - return Set::combine($this->__filterResults($results, true), $keyPath, $valuePath); + return Set::combine($results, $keyPath, $valuePath); break; } } diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 8dd744fe2..a885e65c9 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -452,6 +452,18 @@ class PrimaryModel extends CakeTestModel { class SecondaryModel extends CakeTestModel { var $name = 'SecondaryModel'; } +class JoinA extends CakeTestModel { + var $name = 'JoinA'; + var $hasAndBelongsToMany = array('JoinB', 'JoinC'); +} +class JoinB extends CakeTestModel { + var $name = 'JoinB'; + var $hasAndBelongsToMany = array('JoinA'); +} +class JoinC extends CakeTestModel { + var $name = 'JoinC'; + var $hasAndBelongsToMany = array('JoinA'); +} /** * Short description for class. * @@ -461,14 +473,16 @@ class SecondaryModel extends CakeTestModel { class ModelTest extends CakeTestCase { var $fixtures = array( + 'core.category', 'core.category_thread', 'core.user', 'core.article', 'core.featured', 'core.article_featureds_tags', 'core.article_featured', 'core.articles', 'core.tag', 'core.articles_tag', 'core.comment', 'core.attachment', 'core.apple', 'core.sample', 'core.another_article', 'core.advertisement', 'core.home', 'core.post', 'core.author', 'core.project', 'core.thread', 'core.message', 'core.bid', 'core.portfolio', 'core.item', 'core.items_portfolio', 'core.syfile', 'core.image', 'core.device_type', 'core.device_type_category', 'core.feature_set', 'core.exterior_type_category', 'core.document', 'core.device', 'core.document_directory', - 'core.primary_model', 'core.secondary_model', 'core.something', 'core.something_else', 'core.join_thing' - ); + 'core.primary_model', 'core.secondary_model', 'core.something', 'core.something_else', 'core.join_thing', + 'core.join_a', 'core.join_b', 'core.join_c', 'core.join_a_b', 'core.join_a_c' + ); function start() { parent::start(); @@ -621,6 +635,41 @@ class ModelTest extends CakeTestCase { $this->assertEqual($result, $expected); } + function testSaveMultipleHabtm() { + $this->model = new JoinA(); + $result = $this->model->findById(1); + $expected = array( + 'JoinA' => array('id' => 1, 'name' => 'Join A 1', 'body' => 'Join A 1 Body', 'created' => '2008-01-03 10:54:23', 'updated' => '2008-01-03 10:54:23'), + 'JoinB' => array( + 0 => array('id' => 2, 'name' => 'Join B 2', 'created' => '2008-01-03 10:55:02', 'updated' => '2008-01-03 10:55:02', + 'JoinAsJoinB' => array('id' => 1, 'join_a_id' => 1, 'join_b_id' => 2, 'other' => 'Data for Join A 1 Join B 2', 'created' => '2008-01-03 10:56:33', 'updated' => '2008-01-03 10:56:33'))), + 'JoinC' => array( + 0 => array('id' => 2, 'name' => 'Join C 2', 'created' => '2008-01-03 10:56:12', 'updated' => '2008-01-03 10:56:12', + 'JoinAsJoinC' => array('id' => 1, 'join_a_id' => 1, 'join_c_id' => 2, 'other' => 'Data for Join A 1 Join C 2', 'created' => '2008-01-03 10:57:22', 'updated' => '2008-01-03 10:57:22')))); + + $this->assertEqual($result, $expected); + + $this->model->id = 1; + $data = array( + 'JoinA' => array('id' => '1', 'name' => 'New name for Join A 1'), + 'JoinB' => array(array('id' => 1, 'join_b_id' => 2, 'other' => 'New data for Join A 1 Join B 2')), + 'JoinC' => array(array('id' => 1, 'join_c_id' => 2, 'other' => 'New data for Join A 1 Join C 2'))); + $this->model->set($data); + $this->model->save(); + $ts = date('Y-m-d H:i:s'); + + $result = $this->model->findById(1); + $expected = array( + 'JoinA' => array('id' => 1, 'name' => 'New name for Join A 1', 'body' => 'Join A 1 Body', 'created' => '2008-01-03 10:54:23', 'updated' => $ts), + 'JoinB' => array( + 0 => array('id' => 2, 'name' => 'Join B 2', 'created' => '2008-01-03 10:55:02', 'updated' => '2008-01-03 10:55:02', + 'JoinAsJoinB' => array('id' => 1, 'join_a_id' => 1, 'join_b_id' => 2, 'other' => 'New data for Join A 1 Join B 2', 'created' => $ts, 'updated' => $ts))), + 'JoinC' => array( + 0 => array('id' => 2, 'name' => 'Join C 2', 'created' => '2008-01-03 10:56:12', 'updated' => '2008-01-03 10:56:12', + 'JoinAsJoinC' => array('id' => 1, 'join_a_id' => 1, 'join_c_id' => 2, 'other' => 'New data for Join A 1 Join C 2', 'created' => $ts, 'updated' => $ts)))); + $this->assertEqual($result, $expected); + } + function testFindAllRecursiveSelfJoin() { $this->model =& new Home(); $this->model->recursive = 2; @@ -3172,7 +3221,7 @@ class ModelTest extends CakeTestCase { } function testAfterFindAssociation() { - + } function testDeconstructFields() { diff --git a/cake/tests/fixtures/join_a_b_fixture.php b/cake/tests/fixtures/join_a_b_fixture.php new file mode 100644 index 000000000..891f49d33 --- /dev/null +++ b/cake/tests/fixtures/join_a_b_fixture.php @@ -0,0 +1,53 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.6316 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class JoinABFixture extends CakeTestFixture { + var $name = 'JoinAsJoinB'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary', 'extra'=> 'auto_increment'), + 'join_a_id' => array('type' => 'integer', 'length' => 10, 'null' => true), + 'join_b_id' => array('type' => 'integer', 'default' => ''), + 'other' => array('type' => 'string', 'default' => ''), + 'created' => array('type' => 'datetime', 'null' => true), + 'updated' => array('type' => 'datetime', 'null' => true) + ); + + var $records = array( + array('id' => 1, 'join_a_id' => 1, 'join_b_id' => 2, 'other' => 'Data for Join A 1 Join B 2', 'created' => '2008-01-03 10:56:33', 'updated' => '2008-01-03 10:56:33'), + array('id' => 2, 'join_a_id' => 2, 'join_b_id' => 3, 'other' => 'Data for Join A 2 Join B 3', 'created' => '2008-01-03 10:56:34', 'updated' => '2008-01-03 10:56:34'), + array('id' => 3, 'join_a_id' => 3, 'join_b_id' => 1, 'other' => 'Data for Join A 3 Join B 1', 'created' => '2008-01-03 10:56:35', 'updated' => '2008-01-03 10:56:35') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/join_a_c_fixture.php b/cake/tests/fixtures/join_a_c_fixture.php new file mode 100644 index 000000000..c671e292f --- /dev/null +++ b/cake/tests/fixtures/join_a_c_fixture.php @@ -0,0 +1,53 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.6316 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class JoinACFixture extends CakeTestFixture { + var $name = 'JoinAsJoinC'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary', 'extra'=> 'auto_increment'), + 'join_a_id' => array('type' => 'integer', 'length' => 10, 'null' => true), + 'join_c_id' => array('type' => 'integer', 'default' => ''), + 'other' => array('type' => 'string', 'default' => ''), + 'created' => array('type' => 'datetime', 'null' => true), + 'updated' => array('type' => 'datetime', 'null' => true) + ); + + var $records = array( + array('id' => 1, 'join_a_id' => 1, 'join_c_id' => 2, 'other' => 'Data for Join A 1 Join C 2', 'created' => '2008-01-03 10:57:22', 'updated' => '2008-01-03 10:57:22'), + array('id' => 2, 'join_a_id' => 2, 'join_c_id' => 3, 'other' => 'Data for Join A 2 Join C 3', 'created' => '2008-01-03 10:57:23', 'updated' => '2008-01-03 10:57:23'), + array('id' => 3, 'join_a_id' => 3, 'join_c_id' => 1, 'other' => 'Data for Join A 3 Join C 1', 'created' => '2008-01-03 10:57:24', 'updated' => '2008-01-03 10:57:24') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/join_a_fixture.php b/cake/tests/fixtures/join_a_fixture.php new file mode 100644 index 000000000..979989db6 --- /dev/null +++ b/cake/tests/fixtures/join_a_fixture.php @@ -0,0 +1,52 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.6316 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class JoinAFixture extends CakeTestFixture { + var $name = 'JoinA'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'default' => ''), + 'body' => array('type' => 'text'), + 'created' => array('type' => 'datetime', 'null' => true), + 'updated' => array('type' => 'datetime', 'null' => true) + ); + + var $records = array( + array('id' => 1, 'name' => 'Join A 1', 'body' => 'Join A 1 Body', 'created' => '2008-01-03 10:54:23', 'updated' => '2008-01-03 10:54:23'), + array('id' => 2, 'name' => 'Join A 2', 'body' => 'Join A 2 Body', 'created' => '2008-01-03 10:54:24', 'updated' => '2008-01-03 10:54:24'), + array('id' => 3, 'name' => 'Join A 3', 'body' => 'Join A 2 Body', 'created' => '2008-01-03 10:54:25', 'updated' => '2008-01-03 10:54:24') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/join_b_fixture.php b/cake/tests/fixtures/join_b_fixture.php new file mode 100644 index 000000000..cd54b7be6 --- /dev/null +++ b/cake/tests/fixtures/join_b_fixture.php @@ -0,0 +1,50 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.6316 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class JoinBFixture extends CakeTestFixture { + var $name = 'JoinB'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'default' => ''), + 'created' => array('type' => 'datetime', 'null' => true), + 'updated' => array('type' => 'datetime', 'null' => true) + ); + + var $records = array( + array('id' => 1, 'name' => 'Join B 1', 'created' => '2008-01-03 10:55:01', 'updated' => '2008-01-03 10:55:01'), + array('id' => 2, 'name' => 'Join B 2', 'created' => '2008-01-03 10:55:02', 'updated' => '2008-01-03 10:55:02'), + array('id' => 3, 'name' => 'Join B 3', 'created' => '2008-01-03 10:55:03', 'updated' => '2008-01-03 10:55:03') + ); +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/join_c_fixture.php b/cake/tests/fixtures/join_c_fixture.php new file mode 100644 index 000000000..886dc87c8 --- /dev/null +++ b/cake/tests/fixtures/join_c_fixture.php @@ -0,0 +1,50 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.6316 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class JoinCFixture extends CakeTestFixture { + var $name = 'JoinC'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'default' => ''), + 'created' => array('type' => 'datetime', 'null' => true), + 'updated' => array('type' => 'datetime', 'null' => true) + ); + + var $records = array( + array('id' => 1, 'name' => 'Join C 1', 'created' => '2008-01-03 10:56:11', 'updated' => '2008-01-03 10:56:11'), + array('id' => 2, 'name' => 'Join C 2', 'created' => '2008-01-03 10:56:12', 'updated' => '2008-01-03 10:56:12'), + array('id' => 3, 'name' => 'Join C 3', 'created' => '2008-01-03 10:56:13', 'updated' => '2008-01-03 10:56:13') + ); +} +?> \ No newline at end of file