Cake\ORM\Query::autoFields PHP Méthode

autoFields() public méthode

By default calling select() will disable auto-fields. You can re-enable auto-fields with this method.
public autoFields ( boolean | null $value = null ) : boolean | $this
$value boolean | null The value to set or null to read the current value.
Résultat boolean | $this Either the current value or the query object.
    public function autoFields($value = null)
    {
        if ($value === null) {
            return $this->_autoFields;
        }
        $this->_autoFields = (bool) $value;
        return $this;
    }

Usage Example

 /**
  * Custom finder for distance.
  *
  * Options:
  * - lat (required)
  * - lng (required)
  * - tableName
  * - distance
  * - sort
  *
  * @param \Cake\ORM\Query $query Query.
  * @param array $options Array of options as described above
  * @return \Cake\ORM\Query
  */
 public function findDistance(Query $query, array $options)
 {
     $options += ['tableName' => null, 'sort' => true];
     $sql = $this->distanceExpr($options['lat'], $options['lng'], null, null, $options['tableName']);
     if ($query->autoFields() === null) {
         $query->autoFields(true);
     }
     $query->select(['distance' => $query->newExpr($sql)]);
     if (isset($options['distance'])) {
         // Some SQL versions cannot reuse the select() distance field, so we better reuse the $sql snippet
         $query->where(function ($exp) use($sql, $options) {
             return $exp->lt($sql, $options['distance']);
         });
     }
     if ($options['sort']) {
         $sort = $options['sort'] === true ? 'ASC' : $options['sort'];
         $query->order(['distance' => $sort]);
     }
     return $query;
 }
All Usage Examples Of Cake\ORM\Query::autoFields