/** * Before Fetch command handler. * * Adds joins and where statements. * * @param KModelContextInterface $context The context object. */ protected function _beforeFetch(KModelContextInterface $context) { $query = $context->query; $state = $context->getState(); if (array_intersect(array_keys($state->getValues()), array_keys($this->_columns))) { $table = $this->getRelationsModel()->getTable()->getBase(); $column = $this->getTable()->getIdentityColumn(); $query->join($table . ' AS relations', 'relations.' . $column . ' = tbl.' . $column, 'INNER'); foreach (array_keys($this->_columns) as $column) { if ($state->{$column}) { $query->where(sprintf('relations.%1$s = :%1$s', $column))->bind(array($column => $state->{$column})); } } } }
/** * Add order query * * @param KModelContextInterface $context A model context object * * @return void */ protected function _buildQuery(KModelContextInterface $context) { $model = $context->getSubject(); if ($model instanceof KModelDatabase) { //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)); } } } } }
/** * Add order query * * @param KModelContextInterface $context A model context object * * @return void */ protected function _beforeFetch(KModelContextInterface $context) { $model = $context->getSubject(); if ($model instanceof KModelDatabase && !$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); //if(in_array($column, $columns)) { $context->query->order($column, $direction); //} } if ($sort != 'ordering' && in_array('ordering', $columns)) { $context->query->order('tbl.ordering', 'ASC'); } } }
/** * Add search query * * @param KModelContextInterface $context A model context object * * @return void */ protected function _buildQuery(KModelContextInterface $context) { $model = $context->getSubject(); if ($model instanceof KModelDatabase && !$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 . '%')); } } } }
/** * Add limit query * * @param KModelContextInterface $context A model context object * @return void */ protected function _beforeFetch(KModelContextInterface $context) { $model = $context->getSubject(); if ($model instanceof KModelDatabase && !$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); } } }