Browse Source

Fix an issue where plugin associations would be mishandled.

When joining plugin models together, I ran into an issue when using
`hasMany('Plugin.Model')`. The SQL conditions would be incorrectly
generated as `Plugin.Model.foo_id` instead of `Model.foo_id`.
mark_story 11 years ago
parent
commit
afc591519a

+ 1 - 1
src/ORM/Association/BelongsTo.php

@@ -175,7 +175,7 @@ class BelongsTo extends Association {
  */
 	protected function _linkField($options) {
 		$links = [];
-		$name = $this->name();
+		$name = $this->alias();
 
 		foreach ((array)$this->target()->primaryKey() as $key) {
 			$links[] = sprintf('%s.%s', $name, $key);

+ 1 - 1
src/ORM/Association/HasMany.php

@@ -122,7 +122,7 @@ class HasMany extends Association {
  */
 	protected function _linkField($options) {
 		$links = [];
-		$name = $this->name();
+		$name = $this->alias();
 
 		foreach ((array)$options['foreignKey'] as $key) {
 			$links[] = sprintf('%s.%s', $name, $key);

+ 1 - 1
src/ORM/Association/HasOne.php

@@ -128,7 +128,7 @@ class HasOne extends Association {
  */
 	protected function _linkField($options) {
 		$links = [];
-		$name = $this->name();
+		$name = $this->alias();
 
 		foreach ((array)$options['foreignKey'] as $key) {
 			$links[] = sprintf('%s.%s', $name, $key);

+ 29 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Test\TestCase\ORM;
 
+use Cake\Core\Plugin;
 use Cake\ORM\Query;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
@@ -34,6 +35,7 @@ class QueryRegressionTest extends TestCase {
 	public $fixtures = [
 		'core.user',
 		'core.article',
+		'core.comment',
 		'core.tag',
 		'core.articles_tag',
 		'core.author',
@@ -321,4 +323,31 @@ class QueryRegressionTest extends TestCase {
 		);
 	}
 
+/**
+ * An integration test that spot checks that associations use the
+ * correct alias names to generate queries.
+ *
+ * @return void
+ */
+	public function testPluginAssociationQueryGeneration() {
+		Plugin::load('TestPlugin');
+		$articles = TableRegistry::get('Articles');
+		$articles->hasMany('TestPlugin.Comments');
+		$articles->belongsTo('TestPlugin.Authors');
+
+		$result = $articles->find()
+			->where(['Articles.id' => 2])
+			->contain(['Comments', 'Authors'])
+			->first();
+
+		$this->assertNotEmpty(
+			$result->comments[0]->id,
+			'No SQL error and comment exists.'
+		);
+		$this->assertNotEmpty(
+			$result->author->id,
+			'No SQL error and author exists.'
+		);
+	}
+
 }

+ 24 - 0
tests/test_app/Plugin/TestPlugin/src/Model/Table/AuthorsTable.php

@@ -0,0 +1,24 @@
+<?php
+/**
+ * CakePHP :  Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakefoundation.org/projects/info/cakephp CakePHP Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestPlugin\Model\Table;
+
+use Cake\ORM\Table;
+
+/**
+ * Class AuthorsTable
+ */
+class AuthorsTable extends Table {
+
+}

+ 0 - 1
tests/test_app/Plugin/TestPlugin/src/Model/Table/CommentsTable.php

@@ -18,7 +18,6 @@ use Cake\ORM\Table;
 
 /**
  * Class CommentsTable
- *
  */
 class CommentsTable extends Table {