Skip to content

Commit

Permalink
Using a similar loginc for calculating the name of the pivot table as
Browse files Browse the repository at this point in the history
the one used in 2.x, this will make an easier transition for some users
  • Loading branch information
lorenzo committed Oct 19, 2013
1 parent 700923f commit 26f6a38
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 59 deletions.
13 changes: 7 additions & 6 deletions Cake/ORM/Association/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ public function pivot($table = null) {

if ($table === null) {
if (empty($this->_pivotTable)) {
$table = Table::instance($sAlias . $tAlias);
$table = $table ?: Table::build($sAlias . $tAlias, [
'table' => $this->_joinTableName()
$tableName = $this->_joinTableName();
$tableAlias = Inflector::classify(Inflector::singularize($tableName));
$table = Table::instance($tableAlias);
$table = $table ?: Table::build($tableAlias, [
'table' => $tableName
]);
} else {
return $this->_pivotTable;
Expand Down Expand Up @@ -198,9 +200,8 @@ public function eagerLoader(array $options) {
$fetchQuery = $this->_buildQuery($options);
$resultMap = [];
$key = $options['foreignKey'];
$property = $this->target()->association(
$this->pivot()->alias()
)->property();
$property = $this->target()->association($this->pivot()->alias())->property();

foreach ($fetchQuery->execute() as $result) {
$resultMap[$result[$property][$key]][] = $result;
}
Expand Down
4 changes: 4 additions & 0 deletions Cake/Test/TestApp/Model/Repository/ArticleTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class ArticleTable extends Table {

public function initialize(array $config) {
$this->belongsTo('author');
$this->belongsToMany('tag', [
'property' => 'tags'
]);
$this->hasMany('articleTag');
}

}
88 changes: 44 additions & 44 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testPivot() {
]);
$pivot = $assoc->pivot();
$this->assertInstanceOf('\Cake\ORM\Table', $pivot);
$this->assertEquals('ArticleTag', $pivot->alias());
$this->assertEquals('ArticlesTag', $pivot->alias());
$this->assertEquals('articles_tags', $pivot->table());
$this->assertSame($this->article, $pivot->association('Article')->target());
$this->assertSame($this->tag, $pivot->association('Tag')->target());
Expand All @@ -117,20 +117,20 @@ public function testPivot() {
$this->assertInstanceOf($belongsTo, $pivot->association('Article'));
$this->assertInstanceOf($belongsTo, $pivot->association('Tag'));

$this->assertSame($pivot, $this->tag->association('ArticleTag')->target());
$this->assertSame($pivot, $this->tag->association('ArticlesTag')->target());
$this->assertSame($this->article, $this->tag->association('Article')->target());

$hasMany = '\Cake\ORM\Association\HasMany';
$belongsToMany = '\Cake\ORM\Association\BelongsToMany';
$this->assertInstanceOf($belongsToMany, $this->tag->association('Article'));
$this->assertInstanceOf($hasMany, $this->tag->association('ArticleTag'));
$this->assertInstanceOf($hasMany, $this->tag->association('ArticlesTag'));

$this->assertSame($pivot, $assoc->pivot());
$pivot2 = Table::build('Foo');
$assoc->pivot($pivot2);
$this->assertSame($pivot2, $assoc->pivot());

$assoc->pivot('ArticleTag');
$assoc->pivot('ArticlesTag');
$this->assertSame($pivot, $assoc->pivot());
}

Expand All @@ -146,7 +146,7 @@ public function testPivotWithDefaultTableName() {
'joinTable' => 'tags_articles'
]);
$pivot = $assoc->pivot();
$this->assertEquals('ArticleTag', $pivot->alias());
$this->assertEquals('TagsArticle', $pivot->alias());
$this->assertEquals('tags_articles', $pivot->table());
}

Expand All @@ -163,7 +163,7 @@ public function testAttachTo() {
'targetTable' => $this->tag,
'conditions' => ['Tag.name' => 'cake']
];
Table::build('ArticleTag', [
Table::build('ArticlesTag', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
Expand All @@ -181,10 +181,10 @@ public function testAttachTo() {
]
]);
$query->expects($this->at(2))->method('join')->with([
'ArticleTag' => [
'ArticlesTag' => [
'conditions' => [
'Article.id = ArticleTag.article_id',
'Tag.id = ArticleTag.tag_id'
'Article.id = ArticlesTag.article_id',
'Tag.id = ArticlesTag.tag_id'
],
'type' => 'INNER',
'table' => 'articles_tags'
Expand All @@ -195,8 +195,8 @@ public function testAttachTo() {
'Tag__name' => 'Tag.name',
]);
$query->expects($this->at(3))->method('select')->with([
'ArticleTag__article_id' => 'ArticleTag.article_id',
'ArticleTag__tag_id' => 'ArticleTag.tag_id',
'ArticlesTag__article_id' => 'ArticlesTag.article_id',
'ArticlesTag__tag_id' => 'ArticlesTag.tag_id',
]);
$association->attachTo($query);
}
Expand All @@ -213,7 +213,7 @@ public function testAttachToNoFields() {
'targetTable' => $this->tag,
'conditions' => ['Tag.name' => 'cake']
];
Table::build('ArticleTag', [
Table::build('ArticlesTag', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
Expand All @@ -231,10 +231,10 @@ public function testAttachToNoFields() {
]
]);
$query->expects($this->at(1))->method('join')->with([
'ArticleTag' => [
'ArticlesTag' => [
'conditions' => [
'Article.id = ArticleTag.article_id',
'Tag.id = ArticleTag.tag_id'
'Article.id = ArticlesTag.article_id',
'Tag.id = ArticlesTag.tag_id'
],
'type' => 'INNER',
'table' => 'articles_tags'
Expand All @@ -254,7 +254,7 @@ public function testEagerLoader() {
'sourceTable' => $this->article,
'targetTable' => $this->tag,
];
Table::build('ArticleTag', [
Table::build('ArticlesTag', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
Expand All @@ -267,16 +267,16 @@ public function testEagerLoader() {
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
['id' => 1, 'name' => 'foo', 'ArticleTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticleTag' => ['article_id' => 2]]
['id' => 1, 'name' => 'foo', 'ArticlesTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticlesTag' => ['article_id' => 2]]
];
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'ArticlesTag' => [
'conditions' => ['ArticlesTag.article_id in' => $keys],
'matching' => true
]
])
Expand All @@ -286,14 +286,14 @@ public function testEagerLoader() {
$row = ['Article__id' => 1, 'title' => 'article 1'];
$result = $callable($row);
$row['Tag__Tag'] = [
['id' => 1, 'name' => 'foo', 'ArticleTag' => ['article_id' => 1]]
['id' => 1, 'name' => 'foo', 'ArticlesTag' => ['article_id' => 1]]
];
$this->assertEquals($row, $result);

$row = ['Article__id' => 2, 'title' => 'article 2'];
$result = $callable($row);
$row['Tag__Tag'] = [
['id' => 2, 'name' => 'bar', 'ArticleTag' => ['article_id' => 2]]
['id' => 2, 'name' => 'bar', 'ArticlesTag' => ['article_id' => 2]]
];
$this->assertEquals($row, $result);
}
Expand All @@ -310,7 +310,7 @@ public function testEagerLoaderWithDefaults() {
'conditions' => ['Tag.name' => 'foo'],
'sort' => ['id' => 'ASC'],
];
Table::build('ArticleTag', [
Table::build('ArticlesTag', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
Expand All @@ -324,16 +324,16 @@ public function testEagerLoaderWithDefaults() {
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
['id' => 1, 'name' => 'foo', 'ArticleTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticleTag' => ['article_id' => 2]]
['id' => 1, 'name' => 'foo', 'ArticlesTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticlesTag' => ['article_id' => 2]]
];
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'ArticlesTag' => [
'conditions' => ['ArticlesTag.article_id in' => $keys],
'matching' => true
]
])
Expand Down Expand Up @@ -362,7 +362,7 @@ public function testEagerLoaderWithOverrides() {
'conditions' => ['Tag.name' => 'foo'],
'sort' => ['id' => 'ASC'],
];
Table::build('ArticleTag', [
Table::build('ArticlesTag', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
Expand All @@ -376,16 +376,16 @@ public function testEagerLoaderWithOverrides() {
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
['id' => 1, 'name' => 'foo', 'ArticleTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticleTag' => ['article_id' => 2]]
['id' => 1, 'name' => 'foo', 'ArticlesTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticlesTag' => ['article_id' => 2]]
];
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'ArticlesTag' => [
'conditions' => ['ArticlesTag.article_id in' => $keys],
'matching' => true
]
])
Expand All @@ -405,14 +405,14 @@ public function testEagerLoaderWithOverrides() {
$query->expects($this->once())->method('select')
->with([
'Tag__name' => 'Tag.name',
'ArticleTag__article_id' => 'ArticleTag.article_id'
'ArticlesTag__article_id' => 'ArticlesTag.article_id'
])
->will($this->returnValue($query));

$association->eagerLoader([
'conditions' => ['Tag.id !=' => 3],
'sort' => ['name' => 'DESC'],
'fields' => ['name', 'ArticleTag.article_id'],
'fields' => ['name', 'ArticlesTag.article_id'],
'keys' => $keys,
'query' => $query
]);
Expand All @@ -422,7 +422,7 @@ public function testEagerLoaderWithOverrides() {
* Test the eager loader method with default query clauses
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You are required to select the "ArticleTag.article_id"
* @expectedExceptionMessage You are required to select the "ArticlesTag.article_id"
* @return void
*/
public function testEagerLoaderFieldsException() {
Expand All @@ -432,7 +432,7 @@ public function testEagerLoaderFieldsException() {
'conditions' => ['Tag.name' => 'foo'],
'sort' => ['id' => 'ASC'],
];
Table::build('ArticleTag', [
Table::build('ArticlesTag', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
Expand Down Expand Up @@ -468,7 +468,7 @@ public function testEagerLoaderSubquery() {
'conditions' => ['Tag.name' => 'foo'],
'sort' => ['id' => 'ASC'],
];
Table::build('ArticleTag', [
Table::build('ArticlesTag', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
Expand All @@ -489,8 +489,8 @@ public function testEagerLoaderSubquery() {
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
['id' => 1, 'name' => 'foo', 'ArticleTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticleTag' => ['article_id' => 2]]
['id' => 1, 'name' => 'foo', 'ArticlesTag' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'ArticlesTag' => ['article_id' => 2]]
];
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));
Expand All @@ -504,7 +504,7 @@ public function testEagerLoaderSubquery() {
unset($joins[1]);
$expected
->contain([], true)
->select('ArticleTag.article_id', true)
->select('ArticlesTag.article_id', true)
->join($joins, [], true);

$query->expects($this->once())->method('where')
Expand All @@ -513,8 +513,8 @@ public function testEagerLoaderSubquery() {

$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $expected],
'ArticlesTag' => [
'conditions' => ['ArticlesTag.article_id in' => $expected],
'matching' => true
]
])
Expand All @@ -526,14 +526,14 @@ public function testEagerLoaderSubquery() {
]);

$row['Tag__Tag'] = [
['id' => 1, 'name' => 'foo', 'ArticleTag' => ['article_id' => 1]]
['id' => 1, 'name' => 'foo', 'ArticlesTag' => ['article_id' => 1]]
];
$row['Article__id'] = 1;
$result = $callable($row);
$this->assertEquals($row, $result);

$row['Tag__Tag'] = [
['id' => 2, 'name' => 'bar', 'ArticleTag' => ['article_id' => 2]]
['id' => 2, 'name' => 'bar', 'ArticlesTag' => ['article_id' => 2]]
];
$row['Article__id'] = 2;
$result = $callable($row);
Expand Down
Loading

0 comments on commit 26f6a38

Please sign in to comment.