ソースを参照

Merge pull request #4176 from cakephp/plugin-association

3.0 Fix an issue where plugin associations would be mishandled.
José Lorenzo Rodríguez 11 年 前
コミット
4a8dbbe198

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

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

+ 5 - 2
tests/TestCase/Console/Command/Task/TestTaskTest.php

@@ -181,10 +181,13 @@ class TestTaskTest extends TestCase {
 			->with($this->stringContains('You must provide'));
 			->with($this->stringContains('You must provide'));
 		$this->io->expects($this->at(1))
 		$this->io->expects($this->at(1))
 			->method('out')
 			->method('out')
-			->with($this->stringContains('1. CommentsTable'));
+			->with($this->stringContains('1. AuthorsTable'));
 		$this->io->expects($this->at(2))
 		$this->io->expects($this->at(2))
 			->method('out')
 			->method('out')
-			->with($this->stringContains('2. TestPluginCommentsTable'));
+			->with($this->stringContains('2. CommentsTable'));
+		$this->io->expects($this->at(3))
+			->method('out')
+			->with($this->stringContains('3. TestPluginCommentsTable'));
 
 
 		$this->Task->outputClassChoices('Table');
 		$this->Task->outputClassChoices('Table');
 	}
 	}

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

@@ -14,6 +14,7 @@
  */
  */
 namespace Cake\Test\TestCase\ORM;
 namespace Cake\Test\TestCase\ORM;
 
 
+use Cake\Core\Plugin;
 use Cake\ORM\Query;
 use Cake\ORM\Query;
 use Cake\ORM\Table;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
 use Cake\ORM\TableRegistry;
@@ -34,6 +35,7 @@ class QueryRegressionTest extends TestCase {
 	public $fixtures = [
 	public $fixtures = [
 		'core.user',
 		'core.user',
 		'core.article',
 		'core.article',
+		'core.comment',
 		'core.tag',
 		'core.tag',
 		'core.articles_tag',
 		'core.articles_tag',
 		'core.author',
 		'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
- *
  */
  */
 class CommentsTable extends Table {
 class CommentsTable extends Table {