From 0d3ea7a5f270f14903cfd93af18f70b43fdfdd58 Mon Sep 17 00:00:00 2001 From: phpnut Date: Fri, 12 Dec 2008 17:19:27 +0000 Subject: [PATCH] Adding fix when join table does not have a primary key 'id' defined git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7918 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 2 +- cake/tests/cases/libs/model/model.test.php | 29 ++++++++++++++++++++++ cake/tests/cases/libs/model/models.php | 26 ++++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index d707149da..8e9496c46 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -719,7 +719,7 @@ class Model extends Overloadable { $this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table; } - if ($this->{$joinClass}->primaryKey == 'id' && count($this->{$joinClass}->schema()) <= 2) { + if (count($this->{$joinClass}->schema()) <= 2) { $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 3a5d7ae6e..0f69534c2 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -6238,6 +6238,35 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); } + + function testDeleteArticleBLinks() { + $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); + $TestModel =& new ArticleB(); + + $result = $TestModel->ArticlesTag->find('all'); + $expected = array( + array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')), + array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')), + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) + ); + $this->assertEqual($result, $expected); + + $TestModel->delete(1); + $result = $TestModel->ArticlesTag->find('all'); + + $expected = array( + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) + ); + $this->assertEqual($result, $expected); + } + + function testPkInHabtmLinkModelArticleB() { + $this->loadFixtures('Article', 'Tag'); + $TestModel2 =& new ArticleB(); + $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id'); + } /** * endTest method * diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index e22343e71..b89da27f2 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -2839,4 +2839,28 @@ class TranslatedArticle extends CakeTestModel { */ var $belongsTo = array('User'); } -?> + +class ArticleB extends CakeTestModel { + var $name = 'ArticleB'; + var $useTable = 'articles'; + var $hasAndBelongsToMany = array( + 'TagB' => array( + 'className' => 'TagB', + 'joinTable' => 'articles_tags', + 'foreignKey' => 'article_id', + 'associationForeignKey' => 'tag_id') + ); +} + +class TagB extends CakeTestModel { + var $name = 'TagB'; + var $useTable = 'tags'; + var $hasAndBelongsToMany = array( + 'ArticleB' => array( + 'className' => 'ArticleB', + 'joinTable' => 'articles_tags', + 'foreignKey' => 'tag_id', + 'associationForeignKey' => 'article_id') + ); +} +?> \ No newline at end of file