Skip to content
Permalink
Browse files

Strating to refactor SelectableAssociationTrait into a separate class

  • Loading branch information...
lorenzo committed Oct 10, 2016
1 parent 3a8b28c commit c35c908e46e3e31f17638a85db5365bff8fa8621
@@ -1026,6 +1026,15 @@ public function __call($method, $argument)
*/
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.
*
@@ -37,7 +37,6 @@ class BelongsToMany extends Association
{
use ExternalAssociationTrait {
_options as _externalOptions;
_buildQuery as _buildBaseQuery;
}
@@ -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']);
}
@@ -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']);
}
}
}
@@ -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']);
}
}
}
@@ -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
@@ -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']);
}
}
}
@@ -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;
@@ -29,7 +30,6 @@ class HasOne extends Association
{
use DependentDeleteTrait;
use SelectableAssociationTrait;
/**
* Valid strategies for this type of association
@@ -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;
}
}
Oops, something went wrong.

0 comments on commit c35c908

Please sign in to comment.
You can’t perform that action at this time.