Source of file SelectSource.php
Size: 2,894 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Grid/Source/SelectSource.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Grid\Source; use Bluz\Db; use Bluz\Grid; use Bluz\Proxy; /** * SQL Source Adapter for Grid package * * @package Bluz\Grid * @author Anton Shevchuk */ class SelectSource extends AbstractSource { /** * @var Db\Query\Select instance of select source */ protected $source; /** * Set Select source * * @param Db\Query\Select $source * * @throws Grid\GridException * @return self */ public function setSource($source) { if (!$source instanceof Db\Query\Select) { throw new Grid\GridException('Source of `SelectSource` should be `Db\\Query\\Select` object'); } $this->source = $source; return $this; } /** * Process * * @param array[] $settings * * @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 = (int)Proxy\Db::fetchOne($totalSql); } ); $gridData = new Grid\Data($data); $gridData->setTotal($total); return $gridData; } } |