eagerLoader() public method

Sets the instance of the eager loader class to use for loading associations and storing containments. If called with no arguments, it will return the currently configured instance.
public eagerLoader ( Cake\ORM\EagerLoader $instance = null ) : Cake\ORM\EagerLoader | $this
$instance Cake\ORM\EagerLoader The eager loader to use. Pass null to get the current eagerloader.
return Cake\ORM\EagerLoader | $this
Example #1
0
 /**
  * Get CakePHP property
  *
  * @param string $dataName Column data name
  *
  * @throws Exception
  * @return array|string
  */
 protected function getProperty($dataName)
 {
     $dataName = explode('.', trim($dataName));
     if (count($dataName) != 2) {
         throw new Exception('You are set invalid date.');
     }
     $tableAlias = $dataName[0];
     $colName = $dataName[1];
     if ($this->query->repository()->alias() == $tableAlias) {
         return $colName;
     } elseif (array_key_exists($tableAlias, $this->query->contain())) {
         return ['propertyPath' => $this->query->eagerLoader()->normalized($this->query->repository())[$tableAlias]['propertyPath'], 'field' => $colName];
     }
 }
Example #2
0
 /**
  * Calculates the list of associations that should get eager loaded
  * when fetching each record
  *
  * @return void
  */
 protected function _calculateAssociationMap()
 {
     $contain = $this->_query->eagerLoader()->normalized($this->_defaultTable);
     if (!$contain) {
         return;
     }
     $map = [];
     $visitor = function ($level) use(&$visitor, &$map) {
         foreach ($level as $assoc => $meta) {
             $map[$meta['aliasPath']] = ['alias' => $assoc, 'instance' => $meta['instance'], 'canBeJoined' => $meta['canBeJoined'], 'entityClass' => $meta['instance']->target()->entityClass(), 'nestKey' => $meta['canBeJoined'] ? $assoc : $meta['aliasPath']];
             if ($meta['canBeJoined'] && !empty($meta['associations'])) {
                 $visitor($meta['associations']);
             }
         }
     };
     $visitor($contain, []);
     $this->_associationMap = $map;
 }
Example #3
0
 /**
  * Append a join to the junction table.
  *
  * @param \Cake\ORM\Query $query The query to append.
  * @param string|array $conditions The query conditions to use.
  * @return \Cake\ORM\Query The modified query.
  */
 protected function _appendJunctionJoin($query, $conditions)
 {
     $name = $this->_junctionAssociationName();
     $joins = $query->join();
     $matching = [$name => ['table' => $this->junction()->table(), 'conditions' => $conditions, 'type' => 'INNER']];
     $assoc = $this->target()->association($name);
     $query->addDefaultTypes($assoc->target())->join($matching + $joins, [], true);
     $query->eagerLoader()->addToJoinsMap($name, $assoc);
     return $query;
 }
Example #4
0
 /**
  * Applies all attachable associations to `$query` out of the containments found
  * in the `$surrogate` query.
  *
  * Copies all contained associations from the `$surrogate` query into the
  * passed `$query`. Containments are altered so that they respect the associations
  * chain from which they originated.
  *
  * @param \Cake\ORM\Query $query the query that will get the associations attached to
  * @param \Cake\ORM\Query $surrogate the query having the containments to be attached
  * @param array $options options passed to the method `attachTo`
  * @return void
  */
 protected function _bindNewAssociations($query, $surrogate, $options)
 {
     $loader = $surrogate->eagerLoader();
     $contain = $loader->contain();
     $matching = $loader->matching();
     if (!$contain && !$matching) {
         return;
     }
     $newContain = [];
     foreach ($contain as $alias => $value) {
         $newContain[$options['aliasPath'] . '.' . $alias] = $value;
     }
     $eagerLoader = $query->eagerLoader();
     $eagerLoader->contain($newContain);
     foreach ($matching as $alias => $value) {
         $eagerLoader->matching($options['aliasPath'] . '.' . $alias, $value['queryBuilder'], $value);
     }
 }
 /**
  * Calculates the list of associations that should get eager loaded
  * when fetching each record
  *
  * @return void
  */
 protected function _calculateAssociationMap()
 {
     $map = $this->_query->eagerLoader()->associationsMap($this->_defaultTable);
     $this->_matchingMap = (new Collection($map))->match(['matching' => true])->indexBy('alias')->toArray();
     $this->_containMap = (new Collection(array_reverse($map)))->match(['matching' => false])->indexBy('nestKey')->toArray();
 }
Example #6
0
 /**
  * Applies all attachable associations to `$query` out of the containments found
  * in the `$surrogate` query.
  *
  * Copies all contained associations from the `$surrogate` query into the
  * passed `$query`. Containments are altered so that they respect the associations
  * chain from which they originated.
  *
  * @param \Cake\ORM\Query $query the query that will get the associations attached to
  * @param \Cake\ORM\Query $surrogate the query having the containments to be attached
  * @param array $options options passed to the method `attachTo`
  * @return void
  */
 protected function _bindNewAssociations($query, $surrogate, $options)
 {
     $loader = $surrogate->eagerLoader();
     $contain = $loader->contain();
     $matching = $loader->matching();
     $target = $this->_targetTable;
     if (!$contain && !$matching) {
         return;
     }
     $loader->attachAssociations($query, $target, $options['includeFields']);
     $newContain = [];
     foreach ($contain as $alias => $value) {
         $newContain[$options['aliasPath'] . '.' . $alias] = $value;
     }
     $query->contain($newContain);
     foreach ($matching as $alias => $value) {
         $query->matching($options['aliasPath'] . '.' . $alias, $value['queryBuilder']);
     }
 }
Example #7
0
 /**
  * Applies all attachable associations to `$query` out of the containments found
  * in the `$surrogate` query.
  *
  * Copies all contained associations from the `$surrogate` query into the
  * passed `$query`. Containments are altered so that they respect the associations
  * chain from which they originated.
  *
  * @param \Cake\ORM\Query $query the query that will get the associations attached to
  * @param \Cake\ORM\Query $surrogate the query having the containments to be attached
  * @param array $options options passed to the method `attachTo`
  * @return void
  */
 protected function _bindNewAssociations($query, $surrogate, $options)
 {
     $contain = $surrogate->contain();
     $target = $this->_targetTable;
     if (!$contain) {
         return;
     }
     $loader = $surrogate->eagerLoader();
     $loader->attachAssociations($query, $target, $options['includeFields']);
     $newBinds = [];
     foreach ($contain as $alias => $value) {
         $newBinds[$options['aliasPath'] . '.' . $alias] = $value;
     }
     $query->contain($newBinds);
 }