Rev 3671 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Mapper;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Paginator\Paginator;use LeadersLinked\Mapper\Common\MapperCommon;use LeadersLinked\Model\Post;use LeadersLinked\Hydrator\ObjectPropertyHydrator;use Laminas\Db\ResultSet\HydratingResultSet;use Laminas\Paginator\Adapter\DbSelect;class PostMapper extends MapperCommon {const _TABLE = 'tbl_posts';/**** @var PostMapper*/private static $_instance;/**** @param AdapterInterface $adapter*/private function __construct($adapter){parent::__construct($adapter);}/**** @param AdapterInterface $adapter* @return \LeadersLinked\Mapper\PostMapper*/public static function getInstance($adapter){if(self::$_instance == null) {self::$_instance = new PostMapper($adapter);}return self::$_instance;}/**** @return Post[]*/public function fetchAll(){$prototype = new Post();$select = $this->sql->select(self::_TABLE);return $this->executeFetchAllObject($select, $prototype);}/**** @return Post[]*/public function fetchAllActive(){$prototype = new Post();$select = $this->sql->select(self::_TABLE);$select->where->equalTo('status', Post::STATUS_ACTIVE);$select->order('date DESC');//echo $select->getSqlString($this->adapter->platform);return $this->executeFetchAllObject($select, $prototype);}/**** @param int $network_id* @return Post[]*/public function fetchAllActiveByNetworkId($network_id){$prototype = new Post();$select = $this->sql->select(self::_TABLE);$select->where->equalTo('network_id', $network_id);$select->where->equalTo('status', Post::STATUS_ACTIVE);$select->order('date DESC');//echo $select->getSqlString($this->adapter->platform);return $this->executeFetchAllObject($select, $prototype);}/**** @param int $id* @return Post*/public function fetchOne($id){$prototype = new Post();$select = $this->sql->select(self::_TABLE);$select->where->equalTo('id', $id);return $this->executeFetchOneObject($select, $prototype);}/**** @param string $uuid* @return Post*/public function fetchOneByUuid($uuid){$prototype = new Post();$select = $this->sql->select(self::_TABLE);$select->where->equalTo('uuid', $uuid);return $this->executeFetchOneObject($select, $prototype);}/**** @param string $uuid* @return Post*/public function fetchOneByUuidAnyStatus($uuid){$prototype = new Post();$select = $this->sql->select(self::_TABLE);$select->where->equalTo('uuid', $uuid);return $this->executeFetchOneObject($select, $prototype);}/**** @param string $uuid* @param int $network_id* @return Post*/public function fetchOneByUuidAndNetworkId($uuid, $network_id){$prototype = new Post();$select = $this->sql->select(self::_TABLE);$select->where->equalTo('uuid', $uuid);$select->where->equalTo('network_id', $network_id);return $this->executeFetchOneObject($select, $prototype);}/**** @param Post $post* @return boolean*/public function insert($post){$hydrator = new ObjectPropertyHydrator();$values = $hydrator->extract($post);$values = $this->removeEmpty($values);$insert = $this->sql->insert(self::_TABLE);$insert->values($values);$result = $this->executeInsert($insert);if($result) {$post->id = $this->lastInsertId;}return $result;}/**** @param Post $post* @return boolean*/public function update($post){$hydrator = new ObjectPropertyHydrator();$values = $hydrator->extract($post);$values = $this->removeEmpty($values);$update = $this->sql->update(self::_TABLE);$update->set($values);$update->where->equalTo('id', $post->id);return $this->executeUpdate($update);}/**** @param Post $post* @return boolean*/public function delete($post){$delete = $this->sql->delete(self::_TABLE);$delete->where->equalTo('id', $post->id);return $this->executeDelete($delete);}/**** @param string $search* @param int $network_id* @param int $page* @param int $records_per_page* @param string $order_field* @param string $order_direction* @return Paginator*/public function fetchAllDataTable($search, $network_id, $page = 1, $records_per_page = 10, $order_field= 'title', $order_direction = 'ASC'){$prototype = new Post();$select = $this->sql->select(self::_TABLE);$select->where->notEqualTo('status', Post::STATUS_DELETE);$select->where->equalTo('network_id', $network_id);if($search) {$select->like('title', '%' . $search . '%');}$select->order($order_field . ' ' . $order_direction);$hydrator = new ObjectPropertyHydrator();$resultset = new HydratingResultSet($hydrator, $prototype);$adapter = new DbSelect($select, $this->sql, $resultset);$paginator = new Paginator($adapter);$paginator->setItemCountPerPage($records_per_page);$paginator->setCurrentPageNumber($page);return $paginator;}}