Source of file Table.php
Size: 5,325 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Crud/Table.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Crud; use Bluz\Application\Exception\ApplicationException; use Bluz\Application\Exception\NotFoundException; use Bluz\Common\Singleton; use Bluz\Db; use Bluz\Db\Row; use Bluz\Proxy; /** * Crud Table * * @package Bluz\Crud * @author AntonShevchuk * @link https://github.com/bluzphp/framework/wiki/Crud-Table */ class Table extends AbstractCrud { /** * @var \Bluz\Db\Table instance of Db\Table */ protected $table; /** * Setup Table instance * * @param Db\Table $table * * @return void */ public function setTable(Db\Table $table) { $this->table = $table; } /** * Return table instance for manipulation * * @return Db\Table * @throws ApplicationException */ public function getTable() { if (!$this->table) { $this->initTable(); } return $this->table; } /** * Init table instance for manipulation * * @return void * @throws ApplicationException */ protected function initTable() { $tableClass = class_namespace(static::class) . '\\Table'; // check class initialization if (!class_exists($tableClass) || !is_subclass_of($tableClass, Db\Table::class)) { throw new ApplicationException('`Table` class is not exists or not initialized'); } /** * @var Db\Table $tableClass */ $this->setTable($tableClass::getInstance()); } /** * Get primary key * * @return array */ public function getPrimaryKey() { return $this->getTable()->getPrimaryKey(); } /** * Get record from Db or create new object * * @param mixed $primary * * @return Row * @throws NotFoundException */ public function readOne($primary) { if (!$primary) { return $this->getTable()::create(); } $row = $this->getTable()::findRow($primary); if (!$row) { throw new NotFoundException('Record not found'); } $row = $this->filterRow($row); return $row; } /** * Get set of records * * @param int $offset * @param int $limit * @param array $params * * @return array[Row[], integer] * @throws ApplicationException */ public function readSet($offset = 0, $limit = 10, $params = []) { $select = $this->getTable()::select(); // select only required fields if (count($this->getFields())) { $fields = $this->getFields(); $name = $this->getTable()->getName(); $fields = array_map( function ($field) use ($name) { return $name .'.'. $field; }, $fields ); $select->select(implode(', ', $fields)); } // switch statement for DB type $type = Proxy\Db::getOption('connect', 'type'); switch ($type) { case 'mysql': $selectPart = $select->getQueryPart('select'); $selectPart = 'SQL_CALC_FOUND_ROWS ' . current($selectPart); $select->select($selectPart); $totalSQL = 'SELECT FOUND_ROWS()'; break; case 'pgsql': default: $selectTotal = clone $select; $selectTotal->select('COUNT(*)'); $totalSQL = $selectTotal->getSql(); break; } $select->setLimit($limit); $select->setOffset($offset); $result = []; $total = 0; // run queries // use transaction to avoid errors Proxy\Db::transaction( function () use (&$result, &$total, $select, $totalSQL) { $result = $select->execute(); $total = Proxy\Db::fetchOne($totalSQL); } ); return [$result, $total]; } /** * Create item * * @param array $data * * @return mixed */ public function createOne($data) { $row = $this->getTable()::create(); $data = $this->filterData($data); $row->setFromArray($data); return $row->save(); } /** * Update item * * @param mixed $primary * @param array $data * * @return integer * @throws NotFoundException */ public function updateOne($primary, $data) { $row = $this->getTable()::findRow($primary); if (!$row) { throw new NotFoundException('Record not found'); } $data = $this->filterData($data); $row->setFromArray($data); return $row->save(); } /** * Delete item * * @param mixed $primary * * @return integer * @throws NotFoundException */ public function deleteOne($primary) { $row = $this->getTable()::findRow($primary); if (!$row) { throw new NotFoundException('Record not found'); } return $row->delete(); } } |