Skip to content

Commit

Permalink
Starting to implement select strategy for HasOne
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 2, 2014
1 parent 3d2717c commit 10a501b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/ORM/Association.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ abstract class Association {
*/
const MANY_TO_MANY = 'manyToMany';

/**
* Association type for many to one associations.
*
* @var string
*/
const MANY_TO_ONE = 'manyToOne';

/**
* Name given to the association, it usually represents the alias
* assigned to the target associated table
Expand Down Expand Up @@ -455,11 +462,9 @@ public function transformRow($row, $joined) {
/**
* Get the relationship type.
*
* @return string Constant of either ONE_TO_ONE, MANY_TO_ONE, or MANY_TO_MANY.
* @return string Constant of either ONE_TO_ONE, MANY_TO_ONE, ONE_TO_MANY or MANY_TO_MANY.
*/
public function type() {
return self::ONE_TO_ONE;
}
public abstract function type();

/**
* Proxies the finding operation to the target table's find method
Expand Down
9 changes: 9 additions & 0 deletions src/ORM/Association/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public function isOwningSide(Table $side) {
return $side === $this->target();
}

/**
* Get the relationship type.
*
* @return string
*/
public function type() {
return self::MANY_TO_ONE;
}

/**
* {@inheritdoc}
*
Expand Down
43 changes: 42 additions & 1 deletion src/ORM/Association/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cake\Database\Expression\IdentifierExpression;
use Cake\ORM\Association;
use Cake\ORM\Association\DependentDeleteTrait;
use Cake\ORM\Association\SelectableAssociationTrait;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
Expand All @@ -30,6 +31,7 @@
class HasOne extends Association {

use DependentDeleteTrait;
use SelectableAssociationTrait;

/**
* The type of join to be used when adding the association to a query
Expand Down Expand Up @@ -87,6 +89,15 @@ public function isOwningSide(Table $side) {
return $side === $this->source();
}

/**
* Get the relationship type.
*
* @return string
*/
public function type() {
return self::ONE_TO_ONE;
}

/**
* Takes an entity from the source table and looks if there is a field
* matching the property name for this association. The found entity will be
Expand Down Expand Up @@ -154,7 +165,37 @@ protected function _joinCondition(array $options) {
* {@inheritdoc}
*
*/
public function eagerLoader(array $options) {
protected function _linkField($options) {
$links = [];
$name = $this->name();

foreach ((array)$options['foreignKey'] as $key) {
$links[] = sprintf('%s.%s', $name, $key);
}

if (count($links) === 1) {
return $links[0];
}

return $links;
}

/**
* {@inheritdoc}
*
*/
protected function _buildResultMap($fetchQuery, $options) {
$resultMap = [];
$key = (array)$this->target()->primaryKey();

foreach ($fetchQuery->all() as $result) {
$values = [];
foreach ($key as $k) {
$values[] = $result[$k];
}
$resultMap[implode(';', $values)] = $result;
}
return $resultMap;
}

}
4 changes: 2 additions & 2 deletions src/ORM/Association/SelectableAssociationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function _buildSubquery($query) {

$keys = (array)$query->repository()->primaryKey();

if ($this->type() === $this::ONE_TO_ONE) {
if ($this->type() === $this::MANY_TO_ONE) {
$keys = (array)$this->foreignKey();
}

Expand Down Expand Up @@ -203,7 +203,7 @@ protected abstract function _buildResultMap($fetchQuery, $options);
protected function _resultInjector($fetchQuery, $resultMap) {
$source = $this->source();
$sAlias = $source->alias();
$keys = $this->type() === $this::ONE_TO_ONE ?
$keys = $this->type() === $this::MANY_TO_ONE ?
$this->foreignKey() :
$source->primaryKey();

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/EagerLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ protected function _collectKeys($external, $query, $statement) {
}

$source = $meta['instance']->source();
$keys = $meta['instance']->type() === $meta['instance']::ONE_TO_ONE ?
$keys = $meta['instance']->type() === $meta['instance']::MANY_TO_ONE ?
(array)$meta['instance']->foreignKey() :
(array)$source->primaryKey();

Expand Down
50 changes: 50 additions & 0 deletions tests/TestCase/ORM/CompositeKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,56 @@ public function testBelongsToEager($strategy) {
$this->assertEquals($expected, $results);
}

/**
* Tests loding hasOne with composite keys
*
* @dataProvider internalStategiesProvider
* @return void
*/
public function testHasOneEager($strategy) {
$strategy = 'select';
$table = TableRegistry::get('SiteAuthors');
$table->hasOne('SiteArticles', [
'propertyName' => 'first_article',
'strategy' => $strategy,
'foreignKey' => ['author_id', 'site_id']
]);
$query = new Query($this->connection, $table);
$results = $query->select()
->where(['SiteAuthors.id IN' => [1, 3]])
->contain('SiteArticles')
->hydrate(false)
->toArray();

$expected = [
[
'id' => 1,
'name' => 'mark',
'site_id' => 1,
'first_article' => [
'id' => 1,
'author_id' => 1,
'site_id' => 1,
'title' => 'First Article',
'body' => 'First Article Body'
]
],
[
'id' => 3,
'name' => 'jose',
'site_id' => 2,
'first_article' => [
'id' => 2,
'author_id' => 3,
'site_id' => 2,
'title' => 'Second Article',
'body' => 'Second Article Body'
]
]
];
$this->assertEquals($expected, $results);
}

/**
* Tests that it is possible to insert a new row using the save method
* if the entity has composite primary key
Expand Down

0 comments on commit 10a501b

Please sign in to comment.