Skip to content

Commit

Permalink
Strating to refactor SelectableAssociationTrait into a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 10, 2016
1 parent 3a8b28c commit c35c908
Show file tree
Hide file tree
Showing 7 changed files with 483 additions and 56 deletions.
9 changes: 9 additions & 0 deletions src/ORM/Association.php
Expand Up @@ -1026,6 +1026,15 @@ abstract public function type();
*/
abstract public function eagerLoader(array $options);

/**
* Returns true if the eager loading process will require a set of the owning table's
* binding keys in order to use them as a filter in the finder query.
*
* @param array $options The options containing the strategy to be used.
* @return bool true if a list of keys will be required
*/
abstract function requiresKeys(array $options = []);

/**
* Handles cascading a delete from an associated model.
*
Expand Down
5 changes: 3 additions & 2 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -37,7 +37,6 @@ class BelongsToMany extends Association
{

use ExternalAssociationTrait {
_options as _externalOptions;
_buildQuery as _buildBaseQuery;
}

Expand Down Expand Up @@ -1365,7 +1364,6 @@ protected function _junctionTableName($name = null)
*/
protected function _options(array $opts)
{
$this->_externalOptions($opts);
if (!empty($opts['targetForeignKey'])) {
$this->targetForeignKey($opts['targetForeignKey']);
}
Expand All @@ -1378,5 +1376,8 @@ protected function _options(array $opts)
if (!empty($opts['saveStrategy'])) {
$this->saveStrategy($opts['saveStrategy']);
}
if (isset($opts['sort'])) {
$this->sort($opts['sort']);
}
}
}
13 changes: 0 additions & 13 deletions src/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -123,17 +123,4 @@ protected function _buildResultMap($fetchQuery, $options)

return $resultMap;
}

/**
* Parse extra options passed in the constructor.
*
* @param array $opts original list of options passed in constructor
* @return void
*/
protected function _options(array $opts)
{
if (isset($opts['sort'])) {
$this->sort($opts['sort']);
}
}
}
8 changes: 4 additions & 4 deletions src/ORM/Association/HasMany.php
Expand Up @@ -35,9 +35,7 @@ class HasMany extends Association
{

use DependentDeleteTrait;
use ExternalAssociationTrait {
_options as _externalOptions;
}
use ExternalAssociationTrait;

/**
* The type of join to be used when adding the association to a query
Expand Down Expand Up @@ -534,9 +532,11 @@ public function type()
*/
protected function _options(array $opts)
{
$this->_externalOptions($opts);
if (!empty($opts['saveStrategy'])) {
$this->saveStrategy($opts['saveStrategy']);
}
if (isset($opts['sort'])) {
$this->sort($opts['sort']);
}
}
}
52 changes: 21 additions & 31 deletions src/ORM/Association/HasOne.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Datasource\EntityInterface;
use Cake\ORM\Association;
use Cake\ORM\Association\Loader\SelectLoader;
use Cake\ORM\Table;
use Cake\Utility\Inflector;

Expand All @@ -29,7 +30,6 @@ class HasOne extends Association
{

use DependentDeleteTrait;
use SelectableAssociationTrait;

/**
* Valid strategies for this type of association
Expand Down Expand Up @@ -128,41 +128,31 @@ public function saveAssociated(EntityInterface $entity, array $options = [])
return $entity;
}

/**
* {@inheritDoc}
*/
protected function _linkField($options)
{
$links = [];
$name = $this->alias();

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

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

return $links;
public function eagerLoader(array $options) {
$loader = new SelectLoader([
'alias' => $this->alias(),
'sourceAlias' => $this->source()->alias(),
'targetAlias' => $this->target()->alias(),
'foreignKey' => $this->foreignKey(),
'bindingKey' => $this->bindingKey(),
'strategy' => $this->strategy(),
'associationType' => $this->type(),
'finder' => [$this, 'find']
]);
return $loader->buildLoadingQuery($options);
}

/**
* {@inheritDoc}
* Returns true if the eager loading process will require a set of the owning table's
* binding keys in order to use them as a filter in the finder query.
*
* @param array $options The options containing the strategy to be used.
* @return bool true if a list of keys will be required
*/
protected function _buildResultMap($fetchQuery, $options)
public function requiresKeys(array $options = [])
{
$resultMap = [];
$key = (array)$options['foreignKey'];

foreach ($fetchQuery->all() as $result) {
$values = [];
foreach ($key as $k) {
$values[] = $result[$k];
}
$resultMap[implode(';', $values)] = $result;
}
$strategy = isset($options['strategy']) ? $options['strategy'] : $this->strategy();

return $resultMap;
return $strategy === $this::STRATEGY_SELECT;
}
}

0 comments on commit c35c908

Please sign in to comment.