Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Adding full support for eager loading HasMany when using composite keys
- Loading branch information
Showing
8 changed files
with
274 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : 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://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Test\Fixture; | ||
|
||
use Cake\TestSuite\Fixture\TestFixture; | ||
|
||
class SiteArticleFixture extends TestFixture { | ||
|
||
/** | ||
* fields property | ||
* | ||
* @var array | ||
*/ | ||
public $fields = [ | ||
'id' => ['type' => 'integer'], | ||
'author_id' => ['type' => 'integer', 'null' => true], | ||
'site_id' => ['type' => 'integer', 'null' => true], | ||
'title' => ['type' => 'string', 'null' => true], | ||
'body' => 'text', | ||
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id', 'site_id']]] | ||
]; | ||
|
||
/** | ||
* records property | ||
* | ||
* @var array | ||
*/ | ||
public $records = [ | ||
[ | ||
'id' => 1, | ||
'author_id' => 1, | ||
'site_id' => 1, | ||
'title' => 'First Article', | ||
'body' => 'First Article Body', | ||
], | ||
[ | ||
'id' => 2, | ||
'author_id' => 3, | ||
'site_id' => 2, | ||
'title' => 'Second Article', | ||
'body' => 'Second Article Body', | ||
], | ||
[ | ||
'id' => 3, | ||
'author_id' => 1, | ||
'site_id' => 2, | ||
'title' => 'Third Article', | ||
'body' => 'Third Article Body', | ||
], | ||
[ | ||
'id' => 4, | ||
'author_id' => 3, | ||
'site_id' => 1, | ||
'title' => 'Fourth Article', | ||
'body' => 'Fourth Article Body', | ||
] | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : 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://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Test\Fixture; | ||
|
||
use Cake\TestSuite\Fixture\TestFixture; | ||
|
||
class SiteAuthorFixture extends TestFixture { | ||
|
||
/** | ||
* fields property | ||
* | ||
* @var array | ||
*/ | ||
public $fields = [ | ||
'id' => ['type' => 'integer'], | ||
'name' => ['type' => 'string', 'default' => null], | ||
'site_id' => ['type' => 'integer', 'null' => true], | ||
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id', 'site_id']]] | ||
]; | ||
|
||
/** | ||
* records property | ||
* | ||
* @var array | ||
*/ | ||
public $records = [ | ||
['id' => 1, 'name' => 'mark', 'site_id' => 1], | ||
['id' => 2, 'name' => 'juan', 'site_id' => 2], | ||
['id' => 3, 'name' => 'jose', 'site_id' => 2], | ||
['id' => 4, 'name' => 'andy', 'site_id' => 1] | ||
]; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : 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://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Test\TestCase\ORM; | ||
|
||
use Cake\Database\ConnectionManager; | ||
use Cake\Database\Expression\IdentifierExpression; | ||
use Cake\Database\Expression\OrderByExpression; | ||
use Cake\Database\Expression\QueryExpression; | ||
use Cake\ORM\Query; | ||
use Cake\ORM\ResultSet; | ||
use Cake\ORM\Table; | ||
use Cake\ORM\TableRegistry; | ||
use Cake\TestSuite\TestCase; | ||
|
||
/** | ||
* Integration tetss for table operations involving composite keys | ||
*/ | ||
class CompositeKeyTest extends TestCase { | ||
|
||
/** | ||
* Fixture to be used | ||
* | ||
* @var array | ||
*/ | ||
public $fixtures = ['core.site_article', 'core.site_author']; | ||
|
||
/** | ||
* setUp method | ||
* | ||
* @return void | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$this->connection = ConnectionManager::get('test'); | ||
} | ||
|
||
/** | ||
* Data provider for the two types of strategies HasMany implements | ||
* | ||
* @return void | ||
*/ | ||
public function strategiesProvider() { | ||
return [['subquery'], ['select']]; | ||
} | ||
|
||
/** | ||
* Tests that HasMany associations are correctly eager loaded and results | ||
* correctly nested when multiple foreignKeys are used | ||
* | ||
* @dataProvider strategiesProvider | ||
* @return void | ||
*/ | ||
public function testHasManyEagerCompositeKeys($strategy) { | ||
$table = TableRegistry::get('SiteAuthors'); | ||
TableRegistry::get('SiteArticles'); | ||
$table->hasMany('SiteArticles', [ | ||
'propertyName' => 'articles', | ||
'strategy' => $strategy, | ||
'sort' => ['SiteArticles.id' => 'asc'], | ||
'foreignKey' => ['author_id', 'site_id'] | ||
]); | ||
$query = new Query($this->connection, $table); | ||
|
||
$results = $query->select() | ||
->contain('SiteArticles') | ||
->hydrate(false) | ||
->toArray(); | ||
$expected = [ | ||
[ | ||
'id' => 1, | ||
'name' => 'mark', | ||
'site_id' => 1, | ||
'articles' => [ | ||
[ | ||
'id' => 1, | ||
'title' => 'First Article', | ||
'body' => 'First Article Body', | ||
'author_id' => 1, | ||
'site_id' => 1 | ||
] | ||
] | ||
], | ||
[ | ||
'id' => 2, | ||
'name' => 'juan', | ||
'site_id' => 2 | ||
], | ||
[ | ||
'id' => 3, | ||
'name' => 'jose', | ||
'site_id' => 2, | ||
'articles' => [ | ||
[ | ||
'id' => 2, | ||
'title' => 'Second Article', | ||
'body' => 'Second Article Body', | ||
'author_id' => 3, | ||
'site_id' => 2 | ||
] | ||
] | ||
], | ||
[ | ||
'id' => 4, | ||
'name' => 'andy', | ||
'site_id' => 1 | ||
] | ||
]; | ||
$this->assertEquals($expected, $results); | ||
|
||
$results = $query->repository($table) | ||
->select() | ||
->contain(['SiteArticles' => ['conditions' => ['id' => 2]]]) | ||
->hydrate(false) | ||
->toArray(); | ||
unset($expected[0]['articles']); | ||
$this->assertEquals($expected, $results); | ||
$this->assertEquals($table->association('SiteArticles')->strategy(), $strategy); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters