Bluz\Grid\Source\SelectSource::process PHP Method

process() public method

Process
public process ( array $settings = [] ) : Bluz\Grid\Data
$settings array
return Bluz\Grid\Data
    public function process(array $settings = [])
    {
        // process filters
        if (!empty($settings['filters'])) {
            foreach ($settings['filters'] as $column => $filters) {
                foreach ($filters as $filter => $value) {
                    if ($filter == Grid\Grid::FILTER_LIKE) {
                        $value = '%' . $value . '%';
                    }
                    $this->source->andWhere($column . ' ' . $this->filters[$filter] . ' ?', $value);
                }
            }
        }
        // process orders
        if (!empty($settings['orders'])) {
            // Obtain a list of columns
            foreach ($settings['orders'] as $column => $order) {
                $this->source->addOrderBy($column, $order);
            }
        }
        // process pages
        $this->source->setLimit($settings['limit']);
        $this->source->setPage($settings['page']);
        // prepare query
        $type = Proxy\Db::getOption('connect', 'type');
        if (strtolower($type) == 'mysql') {
            // MySQL
            $select = $this->source->getQueryPart('select');
            $this->source->select('SQL_CALC_FOUND_ROWS ' . current($select));
            $totalSql = 'SELECT FOUND_ROWS()';
        } else {
            // other
            $totalSource = clone $this->source;
            $totalSource->select('COUNT(*)');
            $totalSql = $totalSource->getSql();
        }
        $data = [];
        $total = 0;
        // run queries
        // use transaction to avoid errors
        Proxy\Db::transaction(function () use(&$data, &$total, $totalSql) {
            $data = $this->source->execute();
            $total = Proxy\Db::fetchOne($totalSql);
        });
        $gridData = new Grid\Data($data);
        $gridData->setTotal($total);
        return $gridData;
    }