コード例 #1
1
 /**
  * Custom finder for distance.
  *
  * Options:
  * - lat (required)
  * - lng (required)
  * - tableName
  * - distance
  *
  * @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];
     $sql = $this->distance($options['lat'], $options['lng'], null, null, $options['tableName']);
     $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(['(' . $sql . ') <' => $options['distance']]);
     }
     return $query->order(['distance' => 'ASC']);
 }
コード例 #2
0
ファイル: AppTable.php プロジェクト: pwerken/va-void
 public function beforeFind(Event $event, Query $query, $options, $primary)
 {
     if ($query->clause('limit') == 1) {
         return $query;
     }
     foreach ($this->orderBy() as $field => $ord) {
         $f = $this->aliasField($field);
         $query->order([$this->aliasField($field) => $ord]);
     }
     if (!is_array($this->primaryKey())) {
         return $query;
     }
     $query->sql();
     // force evaluation of internal state/objects
     foreach ($query->clause('join') as $join) {
         if (!$this->association($join['table'])) {
             continue;
         }
         $table = TableRegistry::get($join['table']);
         $table->alias($join['alias']);
         foreach ($table->orderBy() as $field => $ord) {
             $query->order([$table->aliasField($field) => $ord]);
         }
     }
     return $query;
 }
コード例 #3
0
ファイル: FilmsTable.php プロジェクト: array-pop-tarts/flimsy
 public function beforeFind(Event $event, Query $query, \ArrayObject $options, $primary)
 {
     $order = $query->clause('order');
     if ($order === null || !count($order)) {
         $query->order(['Films.released DESC', 'Films.title']);
     }
 }
コード例 #4
0
 public function beforeFind(Event $event, Query $query, \ArrayObject $options, $primary)
 {
     $order = $query->clause('order');
     if ($order === null || !count($order)) {
         $query->order(['Viewers.name']);
     }
 }
コード例 #5
0
ファイル: AppTable.php プロジェクト: mirko-pagliai/me-cms
 /**
  * "Random" find method
  * @param Query $query Query object
  * @param array $options Options
  * @return Query Query object
  */
 public function findRandom(Query $query, array $options)
 {
     $query->order('rand()');
     if (!$query->clause('limit')) {
         $query->limit(1);
     }
     return $query;
 }
コード例 #6
0
 public function findTagged(Query $query, array $options)
 {
     $query->contain(['Users', 'Users.AccountParameters', 'Hashtags']);
     $query->matching('Hashtags', function ($q) use($options) {
         return $q->where(['Hashtags.name' => $options['tag_name']]);
     });
     $query->order(['Tweets.created' => 'DESC']);
     return $query;
 }
コード例 #7
0
ファイル: OrderlyBehavior.php プロジェクト: UseMuffin/Orderly
 /**
  * Add default order clause to query as necessary.
  *
  * @param \Cake\Event\Event $event Event
  * @param \Cake\ORM\Query $query Query
  * @param \ArrayObject $options Options
  * @param bool $primary Boolean indicating whether it's primary query.
  * @return void
  */
 public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
 {
     $orders = $this->_config['orders'];
     $args = [$query, $options, $primary];
     foreach ($orders as $config) {
         if (!empty($config['callback']) && call_user_func_array($config['callback'], $args) || !$query->clause('order')) {
             $query->order($config['order']);
             break;
         }
     }
 }
コード例 #8
0
 /**
  * Retrieve activity records based upon user and timeframe
  * 
  * Requires the pass of the request as the options element
  * pass_params[0] is the number of days to return
  * pass_params[1] is the user, or ALL for all users
  * 
  * If there is an Auth user, and no user param is passed, 
  * the Auth user is used
  * 
  * @param Query $query
  * @param array $options
  * @return Query
  */
 public function findUserActivities(Query $query, array $options)
 {
     $session_user = $options['request']->session()->read('Auth.User.id');
     $default = [0 => 15, 1 => is_null($session_user) ? 'ALL' : $session_user];
     list($days, $user) = $options['request']->params['pass'] + $default;
     $query->order(['Activities.time_in' => 'DESC']);
     $query->contain(['Users', 'Projects', 'Tasks']);
     $query->where(['Activities.time_in >=' => new DateTime("-{$days} days")]);
     if ($user != 'ALL') {
         $query->where(['Activities.user_id' => $user]);
     }
     return $query;
 }
コード例 #9
0
 /**
  * {@inheritDoc}
  */
 public function scope(Query $query, TokenInterface $token)
 {
     $fields = $this->config('fields');
     if ($token->negated() || empty($fields)) {
         return $query;
     }
     $tableAlias = $this->_table->alias();
     $fields = $this->config('fields');
     $value = strtolower($token->value());
     if (is_string($fields)) {
         $fields = [$fields];
     }
     foreach (explode(';', $value) as $segment) {
         $parts = explode(',', $segment);
         if (in_array($parts[0], $fields)) {
             $dir = empty($parts[1]) || !in_array($parts[1], ['asc', 'desc']) ? 'asc' : $parts[1];
             $query->order(["{$tableAlias}.{$parts[0]}" => $dir]);
         }
     }
     return $query;
 }
コード例 #10
0
 public function findPopular(Query $query, array $options)
 {
     $query->limit(isset($options['limit']) ? $options['limit'] : 10);
     $query->order(['Hashtags.counter' => 'DESC']);
     return $query;
 }
コード例 #11
0
 /**
  * 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;
 }
コード例 #12
0
 /**
  * Finder method to get recent requests as a simple array
  *
  * @param Cake\ORM\Query $query The query
  * @param array $options The options
  * @return Query The query.
  */
 public function findRecent(Query $query, array $options)
 {
     return $query->order(['Requests.requested_at' => 'DESC'])->limit(10);
 }
コード例 #13
0
ファイル: CakePHP.php プロジェクト: atkrad/data-tables
 /**
  * Prepare CakePHP order
  *
  * @param Request $request DataTable request
  */
 protected function prepareOrder(Request $request)
 {
     foreach ($request->getOrder() as $order) {
         $this->query->order([$this->table->getColumns()[$order->getColumn()]->getData() => strtoupper($order->getDir())]);
     }
 }
コード例 #14
0
 /**
  * Find latest logs
  * @param Query $query
  * @param array $options limit default 100 record
  */
 public function findLatest(Query $query, array $options)
 {
     return $query->order(['created' => 'DESC'])->limit(isset($options['limit']) ?: 100);
 }
コード例 #15
0
 /**
  * Custom finder for distance.
  *
  * Used to be a virtual field in 2.x via setDistanceAsVirtualField()
  *
  * Options:
  * - lat (required)
  * - lng (required)
  * - tableName
  * - distance
  *
  * @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 += array('tableName' => null);
     $sql = $this->distance($options['lat'], $options['lng'], null, null, $options['tableName']);
     $query->select(['distance' => $query->newExpr($sql)]);
     if (isset($options['distance'])) {
         $query->where(['distance <' => $options['distance']]);
     }
     return $query->order(['distance' => 'ASC']);
 }
コード例 #16
0
 /**
  * Handle datatables sorting parameters to match Query order() accepted parameters.
  *
  * @param  \Cake\ORM\Query   $query Query object
  * @param  \Cake\Event\Event $event The event
  * @return void
  */
 protected function _handleDtSorting(Query $query, Event $event)
 {
     if (!in_array($event->subject()->request->query('format'), [static::FORMAT_DATATABLES])) {
         return;
     }
     if (!$event->subject()->request->query('order')) {
         return;
     }
     $virtualFields = $event->subject()->{$event->subject()->name}->getVirtualFields();
     $sortCol = $event->subject()->request->query('order.0.column') ?: 0;
     $sortDir = $event->subject()->request->query('order.0.dir') ?: 'asc';
     if (!in_array($sortDir, ['asc', 'desc'])) {
         $sortDir = 'asc';
     }
     $fields = $this->_getActionFields($event->subject()->request);
     if (empty($fields)) {
         return;
     }
     // skip if sort column is not found in the action fields
     if (!isset($fields[$sortCol])) {
         return;
     }
     $sortCol = $fields[$sortCol];
     $sortCols = [];
     // handle virtual field
     if (!empty($virtualFields) && isset($virtualFields[$sortCol])) {
         $sortCols = $virtualFields[$sortCol];
     } else {
         $sortCols = (array) $sortCol;
     }
     // prefix table name
     foreach ($sortCols as &$v) {
         $v = $event->subject()->name . '.' . $v;
     }
     // add sort direction to all columns
     $conditions = array_fill_keys($sortCols, $sortDir);
     $query->order($conditions);
 }
コード例 #17
0
ファイル: EventsTable.php プロジェクト: gourmet/dashboard
 /**
  * Find ordered by most recent first.
  *
  * @param \Cake\ORM\Query $query
  * @param array $options
  * @return \Cake\ORM\Query
  */
 public function findRecent(Query $query, array $options)
 {
     return $query->order(['Events.created' => 'DESC'])->limit(10);
 }
コード例 #18
0
 public function findAll(Query $query, array $options)
 {
     return $query->order(['Patterns.day_of_week' => 'ASC']);
 }
コード例 #19
0
 /**
  * Adds order value if not already set in query.
  *
  * @param \Cake\Event\Event $event The beforeFind event that was fired.
  * @param \Cake\ORM\Query $query The query object.
  * @param \ArrayObject $options The options passed to the find method.
  *
  * @return void
  */
 public function beforeFind(Event $event, Query $query, ArrayObject $options)
 {
     if (!$query->clause('order')) {
         $query->order([$this->_table->alias() . '.' . $this->_config['order'] => 'ASC']);
     }
 }
コード例 #20
0
ファイル: HashtagsTable.php プロジェクト: AFPA-Dijon/afpa
 /**
  * Trouve les hashtags les plus populaires
  * @param $query la requête
  * @return la requête
  */
 public function findPopular(Query $query)
 {
     return $query->order(['Hashtags.counter' => 'DESC'])->limit(8);
 }
コード例 #21
0
ファイル: TreeBehavior.php プロジェクト: rlugojr/cakephp
 /**
  * Get the children nodes of the current model
  *
  * Available options are:
  *
  * - for: The id of the record to read.
  * - direct: Boolean, whether to return only the direct (true), or all (false) children,
  *   defaults to false (all children).
  *
  * If the direct option is set to true, only the direct children are returned (based upon the parent_id field)
  *
  * @param \Cake\ORM\Query $query Query.
  * @param array $options Array of options as described above
  * @return \Cake\ORM\Query
  * @throws \InvalidArgumentException When the 'for' key is not passed in $options
  */
 public function findChildren(Query $query, array $options)
 {
     $config = $this->config();
     $options += ['for' => null, 'direct' => false];
     list($parent, $left, $right) = array_map(function ($field) {
         return $this->_table->aliasField($field);
     }, [$config['parent'], $config['left'], $config['right']]);
     list($for, $direct) = [$options['for'], $options['direct']];
     if (empty($for)) {
         throw new InvalidArgumentException("The 'for' key is required for find('children')");
     }
     if ($query->clause('order') === null) {
         $query->order([$left => 'ASC']);
     }
     if ($direct) {
         return $this->_scope($query)->where([$parent => $for]);
     }
     $node = $this->_getNode($for);
     return $this->_scope($query)->where(["{$right} <" => $node->get($config['right']), "{$left} >" => $node->get($config['left'])]);
 }
コード例 #22
0
ファイル: Table.php プロジェクト: dereuromark/cakephp-shim
 /**
  * Sets the default ordering as 2.x shim.
  *
  * If you don't want that, don't call parent when overwriting it in extending classes.
  *
  * @param \Cake\Event\Event $event
  * @param \Cake\ORM\Query $query
  * @param array $options
  * @param bool $primary
  * @return \Cake\ORM\Query
  */
 public function beforeFind(Event $event, Query $query, $options, $primary)
 {
     $order = $query->clause('order');
     if (($order === null || !count($order)) && !empty($this->order)) {
         $query->order($this->order);
     }
     return $query;
 }