From f6d269a9e0d5b7cbad3cd75743b06c637da12cce Mon Sep 17 00:00:00 2001 From: gwoo Date: Thu, 31 Jul 2008 15:38:23 +0000 Subject: [PATCH] closes #5142 primary key on custom join models git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7393 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 12 +-- cake/tests/cases/libs/model/model.test.php | 77 ++++++++++----- cake/tests/cases/libs/model/models.php | 96 ++++++++++++++++++- cake/tests/fixtures/account_fixture.php | 68 +++++++++++++ .../fixtures/content_account_fixture.php | 71 ++++++++++++++ cake/tests/fixtures/content_fixture.php | 68 +++++++++++++ 6 files changed, 357 insertions(+), 35 deletions(-) create mode 100644 cake/tests/fixtures/account_fixture.php create mode 100644 cake/tests/fixtures/content_account_fixture.php create mode 100644 cake/tests/fixtures/content_fixture.php diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 8e55f33bc..d68741179 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -666,17 +666,13 @@ class Model extends Overloadable { 'ds' => $this->useDbConfig )); $this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey']; - } else { $this->__constructLinkedModel($joinClass, $plugin . $joinClass); - $this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey']; - $this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table; - } - if (count($this->{$joinClass}->_schema) > 2) { - if (isset($this->{$joinClass}->_schema['id'])) { - $this->{$joinClass}->primaryKey = 'id'; + if (count($this->{$joinClass}->schema()) <= 2) { + $this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey']; } + $this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table; } } } @@ -1475,7 +1471,7 @@ class Model extends Overloadable { /** * Allows model records to be updated based on a set of conditions * - * @param array $fields Set of fields and values, indexed by fields. + * @param array $fields Set of fields and values, indexed by fields. * Fields are treated as SQL snippets, to insert literal values manually escape your data. * @param mixed $conditions Conditions to match, true for all records * @return boolean True on success, false on failure diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 782708f0d..3418ff451 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -60,7 +60,8 @@ class ModelTest extends CakeTestCase { 'core.document', 'core.device', 'core.document_directory', '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', 'core.uuid', 'core.data_test', 'core.posts_tag', 'core.the_paper_monkies', 'core.person', 'core.underscore_field', - 'core.node', 'core.dependency', 'core.story', 'core.stories_tag', 'core.cd', 'core.book', 'core.overall_favorite' + 'core.node', 'core.dependency', 'core.story', 'core.stories_tag', 'core.cd', 'core.book', 'core.overall_favorite', 'core.account', + 'core.content', 'core.content_account' ); /** * start method @@ -1285,7 +1286,7 @@ class ModelTest extends CakeTestCase { $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id'); $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5); $this->assertEqual($result, $expected); - + $result = $TestModel->updateAll(array('Comment.comment' => "'Updated today'"), array('Comment.user_id' => 5)); $this->assertTrue($result); $result = Set::extract($TestModel->find('all', array('conditions' => array('Comment.user_id' => 5))), '{n}.Comment.comment'); @@ -1763,7 +1764,7 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); unset($TestModel); - + $Author =& new Author(); $Author->Post->bindModel(array( 'hasMany' => array( @@ -1777,12 +1778,12 @@ class ModelTest extends CakeTestCase { 'recursive' => 2 )); $expected = array( - 'id' => 1, - 'article_id' => 1, - 'user_id' => 2, - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', + 'id' => 1, + 'article_id' => 1, + 'user_id' => 2, + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31', 'callback' => 'Fire' ); @@ -2042,7 +2043,7 @@ class ModelTest extends CakeTestCase { $this->assertTrue($result); $result = $TestModel->validates(); $this->assertTrue($result); - + $TestModel->validate = array( 'title' => array( 'tooShort' => array('rule' => array('minLength', 50)), @@ -3150,22 +3151,22 @@ class ModelTest extends CakeTestCase { * * @return void * @access public - */ + */ function testCounterCacheWithSelfJoin() { $this->loadFixtures('CategoryThread'); $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . " ADD column child_count INT(11) DEFAULT '0'"); $Category =& new CategoryThread(); - $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5)); + $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5)); $this->assertTrue($result); - + $Category =& new CategoryThread(); - $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count'; + $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count'; $Category->updateCounterCache(array('parent_id' => 5)); $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count'); $expected = array_fill(0, 1, 1); $this->assertEqual($result, $expected); } - + /** * testSaveWithCounterCacheScope method * @@ -3753,19 +3754,19 @@ class ModelTest extends CakeTestCase { ) ); $this->assertEqual($result, $expected); - + $result = $TestModel->find('all', array('fields' => 'id, name, parent_id', 'conditions' => array('Category.id !=' => 1))); - $expected = array ( - array ('Category' => array('id' => '2', 'name' => 'Category 1.1', 'parent_id' => '1' )), - array ('Category' => array('id' => '3', 'name' => 'Category 1.2', 'parent_id' => '1' )), - array ('Category' => array('id' => '4', 'name' => 'Category 2', 'parent_id' => '0' )), - array ('Category' => array('id' => '5', 'name' => 'Category 3', 'parent_id' => '0' )), - array ('Category' => array('id' => '6', 'name' => 'Category 3.1', 'parent_id' => '5' )), - array ('Category' => array('id' => '7', 'name' => 'Category 1.1.1', 'parent_id' => '2' )), - array ('Category' => array('id' => '8', 'name' => 'Category 1.1.2', 'parent_id' => '2' )), + $expected = array ( + array ('Category' => array('id' => '2', 'name' => 'Category 1.1', 'parent_id' => '1' )), + array ('Category' => array('id' => '3', 'name' => 'Category 1.2', 'parent_id' => '1' )), + array ('Category' => array('id' => '4', 'name' => 'Category 2', 'parent_id' => '0' )), + array ('Category' => array('id' => '5', 'name' => 'Category 3', 'parent_id' => '0' )), + array ('Category' => array('id' => '6', 'name' => 'Category 3.1', 'parent_id' => '5' )), + array ('Category' => array('id' => '7', 'name' => 'Category 1.1.1', 'parent_id' => '2' )), + array ('Category' => array('id' => '8', 'name' => 'Category 1.1.2', 'parent_id' => '2' )), ); $this->assertEqual($result, $expected); - + $result = $TestModel->find('threaded', array('fields' => 'id, parent_id, name', 'conditions' => array('Category.id !=' => 1))); $expected = array( array( @@ -5335,6 +5336,8 @@ class ModelTest extends CakeTestCase { 'day' => '1', 'month' => '1', 'year' => '2008' ), 'title' => 'Test Title', + // schreck - Jul 30, 2008 - should this be set to something else? + 'user_id' => 1 )); $Article->create(); $this->assertTrue($Article->save($data)); @@ -5412,6 +5415,28 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($TestModel->Comment->validationErrors, $expected); } +/** + * testPkInHabtmLinkModel method + * + * @access public + * @return void + */ + function testPkInHabtmLinkModel() { + //Test Nonconformant Models + $this->loadFixtures('Content', 'ContentAccount', 'Account'); + $TestModel =& new Content(); + $this->assertEqual($TestModel->ContentAccount->primaryKey, 'iContentAccountsId'); + + //test conformant models with no PK in the join table + $this->loadFixtures('Article', 'Tag'); + $TestModel2 =& new Article(); + $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id'); + + //test conformant models with PK in join table + $this->loadFixtures('Item', 'Portfolio', 'ItemsPortfolio'); + $TestModel3 =& new Portfolio(); + $this->assertEqual($TestModel3->ItemsPortfolio->primaryKey, 'id'); + } /** * endTest method * @@ -5423,4 +5448,4 @@ class ModelTest extends CakeTestCase { } } -?> +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index 57fb2a969..828de8b2f 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -476,7 +476,7 @@ class ModifiedComment extends CakeTestModel { * @access public */ var $name = 'Comment'; - + var $useTable = 'comments'; /** * belongsTo property @@ -2258,4 +2258,98 @@ class Ad extends CakeTestModel { */ var $belongsTo = array('Campaign'); } +/** + * Nonconformant Content class + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Content extends CakeTestModel { +/** + * name property + * + * @var string 'Content' + * @access public + */ + var $name = 'Content'; +/** + * useTable property + * + * @var string 'Content' + * @access public + */ + var $useTable = 'Content'; +/** + * primaryKey property + * + * @var string 'iContentId' + * @access public + */ + var $primaryKey = 'iContentId'; +/** + * hasAndBelongsToMany property + * + * @var array + * @access public + */ + var $hasAndBelongsToMany = array('Account' => array('className' => 'Account', 'joinTable' => 'ContentAccounts', 'foreignKey' => 'iContentId', 'associationForeignKey', 'iAccountId')); +} +/** + * Nonconformant Account class + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Account extends CakeTestModel { +/** + * name property + * + * @var string 'Account' + * @access public + */ + var $name = 'Account'; +/** + * useTable property + * + * @var string 'Account' + * @access public + */ + var $useTable = 'Accounts'; +/** + * primaryKey property + * + * @var string 'iAccountId' + * @access public + */ + var $primaryKey = 'iAccountId'; +} +/** + * Nonconformant ContentAccount class + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class ContentAccount extends CakeTestModel { +/** + * name property + * + * @var string 'Account' + * @access public + */ + var $name = 'ContentAccount'; +/** + * useTable property + * + * @var string 'Account' + * @access public + */ + var $useTable = 'ContentAccounts'; +/** + * primaryKey property + * + * @var string 'iAccountId' + * @access public + */ + var $primaryKey = 'iContentAccountsId'; +} ?> \ No newline at end of file diff --git a/cake/tests/fixtures/account_fixture.php b/cake/tests/fixtures/account_fixture.php new file mode 100644 index 000000000..cd82bbfa9 --- /dev/null +++ b/cake/tests/fixtures/account_fixture.php @@ -0,0 +1,68 @@ + + * 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.4667 + * @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 AccountFixture extends CakeTestFixture { +/** + * name property + * + * @var string 'Aco' + * @access public + */ + var $name = 'Account'; + var $table = 'Accounts'; +/** + * fields property + * + * @var array + * @access public + */ + var $fields = array( + 'iAccountId' => array('type' => 'integer', 'key' => 'primary'), + 'cDescription' => array('type' => 'string', 'length' => 10, 'null' => true) + ); +/** + * records property + * + * @var array + * @access public + */ + var $records = array( + array('cDescription' => 'gwoo'), + array('cDescription' => 'phpnut'), + array('cDescription' => 'schreck'), + array('cDescription' => 'dude') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/content_account_fixture.php b/cake/tests/fixtures/content_account_fixture.php new file mode 100644 index 000000000..2473ca763 --- /dev/null +++ b/cake/tests/fixtures/content_account_fixture.php @@ -0,0 +1,71 @@ + + * 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.4667 + * @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 ContentAccountFixture extends CakeTestFixture { +/** + * name property + * + * @var string 'Aco' + * @access public + */ + var $name = 'ContentAccount'; + var $table = 'ContentAccounts'; +/** + * fields property + * + * @var array + * @access public + */ + var $fields = array( + 'iContentAccountsId' => array('type' => 'integer', 'key' => 'primary'), + 'iContentId' => array('type' => 'integer'), + 'iAccountId' => array('type' => 'integer') + ); +/** + * records property + * + * @var array + * @access public + */ + var $records = array( + array('iContentId' => 1, 'iAccountId' => 1), + array('iContentId' => 2, 'iAccountId' => 2), + array('iContentId' => 3, 'iAccountId' => 3), + array('iContentId' => 4, 'iAccountId' => 4), + array('iContentId' => 1, 'iAccountId' => 2), + array('iContentId' => 2, 'iAccountId' => 3), + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/content_fixture.php b/cake/tests/fixtures/content_fixture.php new file mode 100644 index 000000000..037168744 --- /dev/null +++ b/cake/tests/fixtures/content_fixture.php @@ -0,0 +1,68 @@ + + * 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.4667 + * @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 ContentFixture extends CakeTestFixture { +/** + * name property + * + * @var string 'Aco' + * @access public + */ + var $name = 'Content'; + var $table = 'Content'; +/** + * fields property + * + * @var array + * @access public + */ + var $fields = array( + 'iContentId' => array('type' => 'integer', 'key' => 'primary'), + 'cDescription' => array('type' => 'string', 'length' => 50, 'null' => true) + ); +/** + * records property + * + * @var array + * @access public + */ + var $records = array( + array('cDescription' => 'Test Content 1'), + array('cDescription' => 'Test Content 2'), + array('cDescription' => 'Test Content 3'), + array('cDescription' => 'Test Content 4') + ); +} + +?> \ No newline at end of file