aliasField() public method

If field is already aliased it will result in no-op.
public aliasField ( string $field ) : string
$field string The field to alias.
return string The field prefixed with the table alias.
Example #1
0
 /**
  * Callback method that listens to the `beforeFind` event in the bound
  * table. It modifies the passed query by eager loading the translated fields
  * and adding a formatter to copy the values into the main table records.
  *
  * @param \Cake\Event\Event $event The beforeFind event that was fired.
  * @param \Cake\ORM\Query $query Query
  * @param \ArrayObject $options The options for the query
  * @return void
  */
 public function beforeFind(Event $event, Query $query, $options)
 {
     $locale = $this->locale();
     if ($locale === $this->config('defaultLocale')) {
         return;
     }
     $conditions = function ($field, $locale, $query, $select) {
         return function ($q) use($field, $locale, $query, $select) {
             $q->where([$q->repository()->aliasField('locale') => $locale]);
             if ($query->autoFields() || in_array($field, $select, true) || in_array($this->_table->aliasField($field), $select, true)) {
                 $q->select(['id', 'content']);
             }
             return $q;
         };
     };
     $contain = [];
     $fields = $this->_config['fields'];
     $alias = $this->_table->alias();
     $select = $query->clause('select');
     $changeFilter = isset($options['filterByCurrentLocale']) && $options['filterByCurrentLocale'] !== $this->_config['onlyTranslated'];
     foreach ($fields as $field) {
         $name = $alias . '_' . $field . '_translation';
         $contain[$name]['queryBuilder'] = $conditions($field, $locale, $query, $select);
         if ($changeFilter) {
             $filter = $options['filterByCurrentLocale'] ? 'INNER' : 'LEFT';
             $contain[$name]['joinType'] = $filter;
         }
     }
     $query->contain($contain);
     $query->formatResults(function ($results) use($locale) {
         return $this->_rowMapper($results, $locale);
     }, $query::PREPEND);
 }
Example #2
0
 /**
  * Builds a query for loading the passed list of entity objects along with the
  * associations specified in $contain.
  *
  * @param Cake\Collection\CollectionInterface $objects The original entitites
  * @param array $contain The associations to be loaded
  * @param Cake\ORM\Table $source The table to use for fetching the top level entities
  * @return Cake\ORM\Query
  */
 protected function _getQuery($objects, $contain, $source)
 {
     $primaryKey = $source->primaryKey();
     $method = is_string($primaryKey) ? 'get' : 'extract';
     $keys = $objects->map(function ($entity) use($primaryKey, $method) {
         return $entity->{$method}($primaryKey);
     });
     $query = $source->find()->select((array) $primaryKey)->where(function ($exp, $q) use($primaryKey, $keys, $source) {
         if (is_array($primaryKey) && count($primaryKey) === 1) {
             $primaryKey = current($primaryKey);
         }
         if (is_string($primaryKey)) {
             return $exp->in($source->aliasField($primaryKey), $keys->toList());
         }
         $types = array_intersect_key($q->defaultTypes(), array_flip($primaryKey));
         $primaryKey = array_map([$source, 'aliasField'], $primaryKey);
         return new TupleComparison($primaryKey, $keys->toList(), $types, 'IN');
     })->contain($contain);
     foreach ($query->eagerLoader()->attachableAssociations($source) as $loadable) {
         $config = $loadable->config();
         $config['includeFields'] = true;
         $loadable->config($config);
     }
     return $query;
 }
Example #3
0
 /**
  * Test that aliasField() works.
  *
  * @return void
  */
 public function testAliasField()
 {
     $table = new Table(['alias' => 'Users']);
     $this->assertEquals('Users.id', $table->aliasField('id'));
 }
 /**
  * Processes the records.
  *
  * @param \Cake\ORM\Table $table
  * @param int $chunkCount
  * @param int $chunkSize
  * @return void
  */
 protected function _process(Table $table, $chunkCount, $chunkSize)
 {
     $query = $table->find();
     if ($table->hasFinder('purifier')) {
         $query->find('purifier');
     }
     $fields = explode(',', $this->param('fields'));
     $fields[] = $table->primaryKey();
     $results = $query->select($fields)->offset($chunkCount)->limit($chunkSize)->orderDesc($table->aliasField($table->primaryKey()))->all();
     if (empty($results)) {
         return;
     }
     foreach ($results as $result) {
         try {
             $table->save($result);
             $chunkCount++;
         } catch (\Exception $e) {
             $this->error($e->getMessage());
         }
     }
 }