Skip to content
Permalink
Browse files

Migrating BelongsTo to use the new SelectLoader class

  • Loading branch information...
lorenzo committed Oct 10, 2016
1 parent c35c908 commit d66a25454f537ee9f8727d39efe44d1a2b9ff139
@@ -17,6 +17,7 @@
use Cake\Database\Expression\IdentifierExpression;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Association;
use Cake\ORM\Association\Loader\SelectLoader;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use RuntimeException;
@@ -30,8 +31,6 @@
class BelongsTo extends Association
{
use SelectableAssociationTrait;
/**
* Valid strategies for this type of association
*
@@ -184,6 +183,38 @@ protected function _joinCondition($options)
return $conditions;
}
/**
* {@inheritDoc}
*
* @return callable
*/
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}
*
* @return bool
*/
public function requiresKeys(array $options = [])
{
$strategy = isset($options['strategy']) ? $options['strategy'] : $this->strategy();
return $strategy === $this::STRATEGY_SELECT;
}
/**
* {@inheritDoc}
*/
@@ -128,6 +128,11 @@ public function saveAssociated(EntityInterface $entity, array $options = [])
return $entity;
}
/**
* {@inheritDoc}
*
* @return callable
*/
public function eagerLoader(array $options) {
$loader = new SelectLoader([
'alias' => $this->alias(),
@@ -139,15 +144,14 @@ public function eagerLoader(array $options) {
'associationType' => $this->type(),
'finder' => [$this, 'find']
]);
return $loader->buildLoadingQuery($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.
* {@inheritDoc}
*
* @param array $options The options containing the strategy to be used.
* @return bool true if a list of keys will be required
* @return bool
*/
public function requiresKeys(array $options = [])
{
@@ -265,8 +265,11 @@ protected function _linkField($options)
{
$links = [];
$name = $this->alias;
$keys = $this->associationType === Association::ONE_TO_ONE ?
$this->foreignKey :
$this->bindingKey;
foreach ((array)$options['foreignKey'] as $key) {
foreach ((array)$keys as $key) {
$links[] = sprintf('%s.%s', $name, $key);
}
@@ -351,7 +354,10 @@ protected function _subqueryFields($query)
protected function _buildResultMap($fetchQuery, $options)
{
$resultMap = [];
$key = (array)$options['foreignKey'];
$keys = $this->associationType === Association::ONE_TO_ONE ?
$this->foreignKey :
$this->bindingKey;
$key = (array)$keys;
foreach ($fetchQuery->all() as $result) {
$values = [];

0 comments on commit d66a254

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