Skip to content
Permalink
Browse files

Fixing the usage of a property name for the pivot table records when

setting up a belongsToMany
  • Loading branch information...
lorenzo committed Oct 19, 2013
1 parent 26f6a38 commit a1889f3ba11298b58c8121cc7eb3516f26a9eb00
@@ -68,6 +68,14 @@ class BelongsToMany extends Association {
*/
protected $_joinTable;
/**
* The name of the hasMany association from the target table
* to the pivot table
*
* @var string
*/
protected $_pivotAssociationName;
/**
* Sets the table instance for the pivot relation. If no arguments
* are passed, the current configured table instance is returned
@@ -220,7 +228,7 @@ public function eagerLoader(array $options) {
*/
protected function _addFilteringCondition($query, $key, $filter) {
return $query->contain([
$this->pivot()->alias() => [
$this->_pivotAssociationName() => [
'conditions' => [$key . ' in' => $filter],
'matching' => true
]
@@ -235,7 +243,23 @@ protected function _addFilteringCondition($query, $key, $filter) {
* @return string
*/
protected function _linkField($options) {
return sprintf('%s.%s', $this->pivot()->alias(), $options['foreignKey']);
return sprintf('%s.%s', $this->_pivotAssociationName(), $options['foreignKey']);
}
/**
* Returns the name of the association from the target table to the pivot table,
* this name is used to generate alias in the query and to later on retrieve the
* results.
*
* @return string
*/
protected function _pivotAssociationName() {
if (!$this->_pivotAssociationName) {
$this->_pivotAssociationName = $this->target()
->association($this->pivot()->alias())
->name();
}
return $this->_pivotAssociationName;
}
/**
@@ -223,7 +223,7 @@ protected function _addFilteringCondition($query, $key, $filter) {
* @return string
*/
protected function _linkField($options) {
return sprintf('%s.%s', $this->target()->alias(), $options['foreignKey']);
return sprintf('%s.%s', $this->name(), $options['foreignKey']);
}
/**
@@ -456,6 +456,7 @@ public function entityClass($name = null) {
* @return Cake\ORM\Association
*/
public function association($name) {
$name = strtolower($name);
if (isset($this->_associations[$name])) {
return $this->_associations[$name];
}
@@ -491,7 +492,7 @@ public function association($name) {
public function belongsTo($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new BelongsTo($associated, $options);
return $this->_associations[$association->name()] = $association;
return $this->_associations[strtolower($association->name())] = $association;
}
/**
@@ -521,7 +522,7 @@ public function belongsTo($associated, array $options = []) {
public function hasOne($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new HasOne($associated, $options);
return $this->_associations[$association->name()] = $association;
return $this->_associations[strtolower($association->name())] = $association;
}
/**
@@ -555,7 +556,7 @@ public function hasOne($associated, array $options = []) {
public function hasMany($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new HasMany($associated, $options);
return $this->_associations[$association->name()] = $association;
return $this->_associations[strtolower($association->name())] = $association;
}
/**
@@ -592,7 +593,7 @@ public function hasMany($associated, array $options = []) {
public function belongsToMany($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new BelongsToMany($associated, $options);
return $this->_associations[$association->name()] = $association;
return $this->_associations[strtolower($association->name())] = $association;
}
/**
@@ -0,0 +1,13 @@
<?php
namespace TestApp\Model\Entity;
use Cake\ORM\Entity;
/**
* Tests entity class used for asserting correct loading
*
*/
class ArticlesTag extends Entity {
}
@@ -0,0 +1,13 @@
<?php
namespace TestApp\Model\Entity;
use Cake\ORM\Entity;
/**
* Tests entity class used for asserting correct loading
*
*/
class Tag extends Entity {
}
@@ -24,7 +24,7 @@ public function initialize(array $config) {
$this->belongsToMany('tag', [
'property' => 'tags'
]);
$this->hasMany('articleTag');
$this->hasMany('articlesTag');
}
}
@@ -0,0 +1,27 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace TestApp\Model\Repository;
use Cake\ORM\Table;
/**
* Tag table class
*
*/
class ArticlesTagTable extends Table {
public function initialize(array $config) {
$this->belongsTo('article');
$this->belongsTo('tag');
}
}
@@ -0,0 +1,28 @@
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace TestApp\Model\Repository;
use Cake\ORM\Table;
/**
* Tag table class
*
*/
class TagTable extends Table {
public function initialize(array $config) {
$this->belongsTo('author');
$this->belongsToMany('tag', ['property' => 'tags']);
$this->hasMany('articlesTag', ['property' => 'extraInfo']);
}
}
@@ -924,4 +924,17 @@ public function testReciprocalHasManyLoading() {
}
}
/**
* Tests that the correct table and entity are loaded for the pivot association in
* a belongsToMany setup
*
* @return void
*/
public function testReciprocalBelongsToMany() {
$table = new \TestApp\Model\Repository\ArticleTable;
$result = $table->find('all')->contain(['tag'])->first();
$this->assertInstanceOf('TestApp\Model\Entity\Tag', $result->tags[0]);
$this->assertInstanceOf('TestApp\Model\Entity\ArticlesTag', $result->tags[0]->extraInfo);
}
}

0 comments on commit a1889f3

Please sign in to comment.
You can’t perform that action at this time.