"Fixes #3814, Bug in hasAndBelongsToMany"

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6317 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2008-01-03 18:50:59 +00:00
parent d963a721b3
commit 6dec91a89f
7 changed files with 312 additions and 4 deletions

View file

@ -1266,6 +1266,7 @@ class Model extends Overloadable {
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db =& ConnectionManager::getDataSource($this->useDbConfig);
foreach ($joined as $assoc => $value) { foreach ($joined as $assoc => $value) {
$newValues = array();
if (isset($this->hasAndBelongsToMany[$assoc])) { if (isset($this->hasAndBelongsToMany[$assoc])) {
list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']); list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']);
$conditions = array($this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id); $conditions = array($this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id);
@ -1776,7 +1777,7 @@ class Model extends Overloadable {
if (empty($results)) { if (empty($results)) {
return array(); return array();
} }
return Set::combine($this->__filterResults($results, true), $keyPath, $valuePath); return Set::combine($results, $keyPath, $valuePath);
break; break;
} }
} }

View file

@ -452,6 +452,18 @@ class PrimaryModel extends CakeTestModel {
class SecondaryModel extends CakeTestModel { class SecondaryModel extends CakeTestModel {
var $name = 'SecondaryModel'; 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. * Short description for class.
* *
@ -461,14 +473,16 @@ class SecondaryModel extends CakeTestModel {
class ModelTest extends CakeTestCase { class ModelTest extends CakeTestCase {
var $fixtures = array( var $fixtures = array(
'core.category', 'core.category_thread', 'core.user', 'core.article', 'core.featured', 'core.article_featureds_tags', '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.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.apple', 'core.sample', 'core.another_article', 'core.advertisement', 'core.home', 'core.post', 'core.author',
'core.project', 'core.thread', 'core.message', 'core.bid', 'core.project', 'core.thread', 'core.message', 'core.bid',
'core.portfolio', 'core.item', 'core.items_portfolio', 'core.syfile', 'core.image', '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.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() { function start() {
parent::start(); parent::start();
@ -621,6 +635,41 @@ class ModelTest extends CakeTestCase {
$this->assertEqual($result, $expected); $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() { function testFindAllRecursiveSelfJoin() {
$this->model =& new Home(); $this->model =& new Home();
$this->model->recursive = 2; $this->model->recursive = 2;
@ -3172,7 +3221,7 @@ class ModelTest extends CakeTestCase {
} }
function testAfterFindAssociation() { function testAfterFindAssociation() {
} }
function testDeconstructFields() { function testDeconstructFields() {

View file

@ -0,0 +1,53 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* 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')
);
}
?>

View file

@ -0,0 +1,53 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* 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')
);
}
?>

52
cake/tests/fixtures/join_a_fixture.php vendored Normal file
View file

@ -0,0 +1,52 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* 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')
);
}
?>

50
cake/tests/fixtures/join_b_fixture.php vendored Normal file
View file

@ -0,0 +1,50 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* 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')
);
}
?>

50
cake/tests/fixtures/join_c_fixture.php vendored Normal file
View file

@ -0,0 +1,50 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* 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')
);
}
?>