From b063f22f3133cc3f91dfa3d0f7017179c4e348b3 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 23 Dec 2008 23:28:08 +0000 Subject: [PATCH] Fixing habtm joins for tables with two uuid fields, no primary key and a defined join model. Adding fixtures. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7953 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 4 +- cake/tests/cases/libs/model/model.test.php | 26 +++++++- cake/tests/fixtures/fruit_fixture.php | 64 +++++++++++++++++++ .../fixtures/fruits_uuid_tag_fixture.php | 64 +++++++++++++++++++ cake/tests/fixtures/uuid_tag_fixture.php | 62 ++++++++++++++++++ 5 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 cake/tests/fixtures/fruit_fixture.php create mode 100644 cake/tests/fixtures/fruits_uuid_tag_fixture.php create mode 100644 cake/tests/fixtures/uuid_tag_fixture.php diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index f3f9eb721..b40f35b1b 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -714,12 +714,12 @@ class Model extends Overloadable { 'table' => $this->{$type}[$assocKey]['joinTable'], 'ds' => $this->useDbConfig )); - } else { + } else { $this->__constructLinkedModel($joinClass, $plugin . $joinClass); $this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table; } - if (count($this->{$joinClass}->schema()) <= 2) { + if (count($this->{$joinClass}->schema()) <= 2 && $this->{$joinClass}->primaryKey !== false) { $this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey']; } } diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index b588e86a5..b8534aedb 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -66,7 +66,8 @@ class ModelTest extends CakeTestCase { 'core.overall_favorite', 'core.account', 'core.content', 'core.content_account', 'core.film_file', 'core.test_plugin_article', 'core.test_plugin_comment', 'core.uuiditem', 'core.counter_cache_user', 'core.counter_cache_post', 'core.uuidportfolio', - 'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid' + 'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid', + 'core.fruit', 'core.fruits_uuid_tag', 'core.uuid_tag' ); /** * start method @@ -310,6 +311,29 @@ class ModelTest extends CakeTestCase { $result = $TestModel->read(null, $id); $this->assertEqual(1, count($result['Uuiditem'])); } +/** + * test HABTM saving when join table has no primary key and only 2 columns. + * + * @return void + **/ + function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() { + $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); + $Fruit =& new Fruit(); + $data = array( + 'Fruit' => array( + 'color' => 'Red', + 'shape' => 'Heart-shaped', + 'taste' => 'sweet', + 'name' => 'Strawberry', + ), + 'UuidTag' => array( + 'UuidTag' => array( + '481fc6d0-b920-43e0-e50f-6d1740cf8569' + ) + ) + ); + $this->assertTrue($Fruit->save($data)); + } /** * testHabtmUuidWithNumericId method * diff --git a/cake/tests/fixtures/fruit_fixture.php b/cake/tests/fixtures/fruit_fixture.php new file mode 100644 index 000000000..62034bd0e --- /dev/null +++ b/cake/tests/fixtures/fruit_fixture.php @@ -0,0 +1,64 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @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.7953 + * @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 FruitFixture extends CakeTestFixture { +/** + * name property + * + * @var string 'Fruit' + * @access public + */ + var $name = 'Fruit'; +/** + * fields property + * + * @var array + * @access public + */ + var $fields = array( + 'id' => array('type' => 'string', 'length' => 36, 'key' => 'primary'), + 'name' => array('type' => 'string', 'length' => 255), + 'color' => array('type' => 'string', 'length' => 13), + 'shape' => array('type' => 'string', 'length' => 255), + 'taste' => array('type' => 'string', 'length' => 255) + ); +/** + * records property + * + * @var array + * @access public + */ + var $records = array( + array('id' => '481fc6d0-b920-43e0-a40d-6d1740cf8569', 'name' => 'Orange', 'color' => 'orange', 'shape' => 'Spherical', 'taste' => 'Tangy & Sweet') + ); +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/fruits_uuid_tag_fixture.php b/cake/tests/fixtures/fruits_uuid_tag_fixture.php new file mode 100644 index 000000000..bc0fce628 --- /dev/null +++ b/cake/tests/fixtures/fruits_uuid_tag_fixture.php @@ -0,0 +1,64 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @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.7953 + * @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 FruitsUuidTagFixture extends CakeTestFixture { +/** + * name property + * + * @var string 'FruitsUuidTag' + * @access public + */ + var $name = 'FruitsUuidTag'; +/** + * fields property + * + * @var array + * @access public + */ + var $fields = array( + 'fruit_id' => array('type' => 'string', 'null' => false, 'length' => 36, 'key' => 'primary'), + 'uuid_tag_id' => array('type' => 'string', 'null' => false, 'length' => 36, 'key' => 'primary'), + 'indexes' => array( + 'unique_fruits_tags' => array('unique' => true, 'column' => array('fruit_id', 'uuid_tag_id')), + ), + ); +/** + * records property + * + * @var array + * @access public + */ + var $records = array( + array('fruit_id' => '481fc6d0-b920-43e0-a40d-6d1740cf8569', 'uuid_tag_id' => '481fc6d0-b920-43e0-e50f-6d1740cf8569') + ); +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/uuid_tag_fixture.php b/cake/tests/fixtures/uuid_tag_fixture.php new file mode 100644 index 000000000..0eb3b2b88 --- /dev/null +++ b/cake/tests/fixtures/uuid_tag_fixture.php @@ -0,0 +1,62 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @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.7953 + * @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 UuidTagFixture extends CakeTestFixture { +/** + * name property + * + * @var string 'UuidTag' + * @access public + */ + var $name = 'UuidTag'; +/** + * fields property + * + * @var array + * @access public + */ + var $fields = array( + 'id' => array('type' => 'string', 'length' => 36, 'key' => 'primary'), + 'name' => array('type' => 'string', 'length' => 255), + 'created' => array('type' => 'datetime') +); +/** + * records property + * + * @var array + * @access public + */ + var $records = array( + array('id' => '481fc6d0-b920-43e0-e50f-6d1740cf8569', 'name' => 'MyTag', 'created' => '2009-12-09 12:30:00') + ); +} +?> \ No newline at end of file