fixes #5239, primary key for HABTM, reverts last change for #5233 which broke the tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7454 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-08-11 16:18:19 +00:00
parent 460223b61a
commit 995040a98a
3 changed files with 44 additions and 7 deletions

View file

@ -300,7 +300,7 @@ class ContainableBehavior extends ModelBehavior {
continue;
}
$optionKey = in_array($key, $options, true);
if (!$optionKey && is_string($key) && preg_match('/^[a-zA-Z(]/', $key) && (!isset($Model->{$key}) || !is_object($Model->{$key}))) {
if (!$optionKey && is_string($key) && preg_match('/^[a-z(]/', $key) && (!isset($Model->{$key}) || !is_object($Model->{$key}))) {
$option = 'fields';
$val = array($key);
if ($key{0} == '(') {

View file

@ -646,7 +646,7 @@ class Model extends Overloadable {
}
}
if (isset($this->{$type}[$assocKey]['with']) && !empty($this->{$type}[$assocKey]['with'])) {
if (!empty($this->{$type}[$assocKey]['with'])) {
$joinClass = $this->{$type}[$assocKey]['with'];
if (is_array($joinClass)) {
$joinClass = key($joinClass);
@ -665,15 +665,14 @@ class Model extends Overloadable {
'table' => $this->{$type}[$assocKey]['joinTable'],
'ds' => $this->useDbConfig
));
$this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey'];
} else {
$this->__constructLinkedModel($joinClass, $plugin . $joinClass);
if (count($this->{$joinClass}->schema()) <= 2) {
$this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey'];
}
$this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table;
}
if ($this->{$joinClass}->primaryKey == 'id' && count($this->{$joinClass}->schema()) <= 2) {
$this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey'];
}
}
}
}

View file

@ -5545,6 +5545,44 @@ class ModelTest extends CakeTestCase {
$this->loadFixtures('Item', 'Portfolio', 'ItemsPortfolio');
$TestModel3 =& new Portfolio();
$this->assertEqual($TestModel3->ItemsPortfolio->primaryKey, 'id');
//test conformant models with PK in join table - join table contains extra field
$this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
$TestModel4 =& new JoinA();
$this->assertEqual($TestModel4->JoinAsJoinB->primaryKey, 'id');
}
/**
* testInsertAnotherHabtmRecordWithSameForeignKey method
*
* @access public
* @return void
*/
function testInsertAnotherHabtmRecordWithSameForeignKey() {
$this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
$TestModel = new JoinA();
$result = $TestModel->JoinAsJoinB->findById(1);
$expected = array('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'));
$this->assertEqual($result, $expected);
$TestModel->JoinAsJoinB->create();
$result = $TestModel->JoinAsJoinB->save(array('join_a_id' => 1, 'join_b_id' => 1, 'other' => 'Data for Join A 1 Join B 1', 'created' => '2008-01-03 10:56:44', 'updated' => '2008-01-03 10:56:44'));
$this->assertTrue($result);
$lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
$this->assertTrue($lastInsertId != null);
$result = $TestModel->JoinAsJoinB->findById(1);
$expected = array('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'));
$this->assertEqual($result, $expected);
$updatedValue = 'UPDATED Data for Join A 1 Join B 2';
$TestModel->JoinAsJoinB->id = 1;
$result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false);
$this->assertTrue($result);
$result = $TestModel->JoinAsJoinB->findById(1);
$this->assertEqual($result['JoinAsJoinB']['other'], $updatedValue);
}
/**
* endTest method