Esempio n. 1
0
 /**
  * Add order query
  *
  * @param   ModelContextInterface $context A model context object
  *
  * @return    void
  */
 protected function _buildQuery(ModelContextInterface $context)
 {
     $model = $context->getSubject();
     if ($model instanceof ModelDatabase) {
         //Get only the unique states
         $states = $context->state->getValues(true);
         if (!empty($states)) {
             $columns = array_intersect_key($states, $model->getTable()->getColumns());
             $columns = $model->getTable()->mapColumns($columns);
             foreach ($columns as $column => $value) {
                 if (isset($value)) {
                     $context->query->where('tbl.' . $column . ' ' . (is_array($value) ? 'IN' : '=') . ' :' . $column)->bind(array($column => $value));
                 }
             }
         }
     }
 }
Esempio n. 2
0
 /**
  * Add order query
  *
  * @param   ModelContextInterface $context A model context object
  * @return  void
  */
 protected function _beforeFetch(ModelContextInterface $context)
 {
     $model = $context->getSubject();
     if ($model instanceof ModelDatabase && !$context->state->isUnique()) {
         $state = $context->state;
         $sort = $state->sort;
         $direction = strtoupper($state->direction);
         $columns = array_keys($this->getTable()->getColumns());
         if ($sort) {
             $column = $this->getTable()->mapColumns($sort);
             $context->query->order($column, $direction);
         }
         if ($sort != 'ordering' && in_array('ordering', $columns)) {
             $context->query->order('tbl.ordering', 'ASC');
         }
     }
 }
Esempio n. 3
0
 /**
  * Add search query
  *
  * @param   ModelContextInterface $context A model context object
  *
  * @return    void
  */
 protected function _buildQuery(ModelContextInterface $context)
 {
     $model = $context->getSubject();
     if ($model instanceof ModelDatabase && !$context->state->isUnique()) {
         $state = $context->state;
         $search = $state->search;
         if ($search) {
             $columns = array_keys($this->getTable()->getColumns());
             $conditions = array();
             foreach ($this->_columns as $column) {
                 if (in_array($column, $columns)) {
                     $conditions[] = 'tbl.' . $column . ' LIKE :search';
                 }
             }
             if ($conditions) {
                 $context->query->where('(' . implode(' OR ', $conditions) . ')')->bind(array('search' => '%' . $search . '%'));
             }
         }
     }
 }
Esempio n. 4
0
 /**
  * Add limit query
  *
  * @param   ModelContextInterface $context A model context object
  * @return    void
  */
 protected function _beforeFetch(ModelContextInterface $context)
 {
     $model = $context->getSubject();
     if ($model instanceof ModelDatabase && !$context->state->isUnique()) {
         $state = $context->state;
         $limit = $state->limit;
         if ($limit) {
             $offset = $state->offset;
             $total = $this->count();
             if ($offset !== 0 && $total !== 0) {
                 // Recalculate the offset if it is set to the middle of a page.
                 if ($offset % $limit !== 0) {
                     $offset -= $offset % $limit;
                 }
                 // Recalculate the offset if it is higher than the total
                 if ($offset >= $total) {
                     $offset = floor(($total - 1) / $limit) * $limit;
                 }
                 $state->offset = $offset;
             }
             $context->query->limit($limit, $offset);
         }
     }
 }