Skip to content

Commit

Permalink
Simplifying code and make assotiation tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 6, 2013
1 parent c47e6b9 commit a88f451
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
5 changes: 2 additions & 3 deletions Cake/ORM/Association/BelongsTo.php
Expand Up @@ -16,7 +16,6 @@
*/
namespace Cake\ORM\Association;

use Cake\Database\Expression\Comparison;
use Cake\Database\Expression\FieldExpression;
use Cake\ORM\Association;
use Cake\ORM\Entity;
Expand Down Expand Up @@ -73,7 +72,7 @@ public function cascadeDelete(Entity $entity, $options = []) {
* clause for getting the results on the target table.
*
* @param array $options list of options passed to attachTo method
* @return boolean|QueryExpression
* @return array
*/
protected function _joinCondition(array $options) {
$field = sprintf(
Expand All @@ -86,7 +85,7 @@ protected function _joinCondition(array $options) {
$this->_sourceTable->alias(),
$options['foreignKey']
));
return new Comparison($field, $value, 'string', '=');
return [$field => $value];
}

}
2 changes: 1 addition & 1 deletion Cake/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -133,7 +133,7 @@ protected function _joinCondition(array $options) {
$this->_targetTable->alias(),
$options['foreignKey']
));
return new Comparison($field, $value, 'string', '=');
return [$field => $value];
}

/**
Expand Down
17 changes: 13 additions & 4 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\FieldExpression;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Entity;
use Cake\ORM\Query;
Expand Down Expand Up @@ -183,11 +184,15 @@ public function testAttachTo() {
'table' => 'tags'
]
]);

$field1 = new FieldExpression('ArticlesTag.article_id');
$field2 = new FieldExpression('ArticlesTag.tag_id');

$query->expects($this->at(2))->method('join')->with([
'ArticlesTag' => [
'conditions' => [
'Article.id = ArticlesTag.article_id',
'Tag.id = ArticlesTag.tag_id'
['Article.id' => $field1],
['Tag.id' => $field2]
],
'type' => 'INNER',
'table' => 'articles_tags'
Expand Down Expand Up @@ -233,11 +238,15 @@ public function testAttachToNoFields() {
'table' => 'tags'
]
]);

$field1 = new FieldExpression('ArticlesTag.article_id');
$field2 = new FieldExpression('ArticlesTag.tag_id');

$query->expects($this->at(1))->method('join')->with([
'ArticlesTag' => [
'conditions' => [
'Article.id = ArticlesTag.article_id',
'Tag.id = ArticlesTag.tag_id'
['Article.id' => $field1],
['Tag.id' => $field2]
],
'type' => 'INNER',
'table' => 'articles_tags'
Expand Down
7 changes: 5 additions & 2 deletions Cake/Test/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\FieldExpression;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Entity;
use Cake\ORM\Query;
Expand Down Expand Up @@ -85,11 +86,12 @@ public function testAttachTo() {
'conditions' => ['Company.is_active' => true]
];
$association = new BelongsTo('Company', $config);
$field = new FieldExpression('Client.company_id');
$query->expects($this->once())->method('join')->with([
'Company' => [
'conditions' => [
'Company.is_active' => true,
'Company.id = Client.company_id',
['Company.id' => $field]
],
'table' => 'companies',
'type' => 'LEFT'
Expand Down Expand Up @@ -149,11 +151,12 @@ public function testAttachToNoFields() {
'conditions' => ['Company.is_active' => true]
];
$association = new BelongsTo('Company', $config);
$field = new FieldExpression('Client.company_id');
$query->expects($this->once())->method('join')->with([
'Company' => [
'conditions' => [
'Company.is_active' => true,
'Company.id = Client.company_id',
['Company.id' => $field]
],
'type' => 'LEFT',
'table' => 'companies',
Expand Down
7 changes: 5 additions & 2 deletions Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\FieldExpression;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Entity;
use Cake\ORM\Query;
Expand Down Expand Up @@ -353,12 +354,14 @@ public function testAttachTo() {
'targetTable' => $this->article,
'conditions' => ['Article.is_active' => true]
];

$field = new FieldExpression('Article.author_id');
$association = new HasMany('Article', $config);
$query->expects($this->once())->method('join')->with([
'Article' => [
'conditions' => [
'Article.is_active' => true,
'Author.id = Article.author_id',
['Author.id' => $field]
],
'type' => 'INNER',
'table' => 'articles'
Expand Down Expand Up @@ -423,7 +426,7 @@ public function testAttachToNoFields() {
'Article' => [
'conditions' => [
'Article.is_active' => true,
'Author.id = Article.author_id',
['Author.id' => $field]
],
'type' => 'INNER',
'table' => 'articles'
Expand Down

0 comments on commit a88f451

Please sign in to comment.