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
This commit is contained in:
gwoo 2008-07-31 15:38:23 +00:00
parent f97a3f27b0
commit f6d269a9e0
6 changed files with 357 additions and 35 deletions

View file

@ -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;
}
}
}

View file

@ -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
@ -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
*

View file

@ -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';
}
?>

68
cake/tests/fixtures/account_fixture.php vendored Normal file
View file

@ -0,0 +1,68 @@
<?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.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')
);
}
?>

View file

@ -0,0 +1,71 @@
<?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.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),
);
}
?>

68
cake/tests/fixtures/content_fixture.php vendored Normal file
View file

@ -0,0 +1,68 @@
<?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.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')
);
}
?>