AutorÃa | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Mapper;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Paginator\Adapter\DbSelect;
use Laminas\Paginator\Paginator;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Model\CompanyService;
use LeadersLinked\Mapper\Common\MapperCommon;
class CompanyServiceMapper extends MapperCommon
{
const _TABLE = 'tbl_company_services';
/**
*
* @var CompanyServiceMapper
*/
private static $_instance;
/**
*
* @param AdapterInterface $adapter
*/
private function __construct($adapter)
{
parent::__construct($adapter);
}
/**
*
* @param AdapterInterface $adapter
* @return CompanyServiceMapper
*/
public static function getInstance($adapter)
{
if(self::$_instance == null) {
self::$_instance = new CompanyServiceMapper($adapter);
}
return self::$_instance;
}
/**
*
* @param int $id
* @return CompanyService
*/
public function fetchOne($id)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('id', $id);
$select->limit(1);
$prototype = new CompanyService();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $company_id
* @return CompanyService[]
*/
public function fetchAllByCompanyId($company_id)
{
$prototype = new CompanyService();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('company_id', $company_id);
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param int $service_id
* @return CompanyService[]
*/
public function fetchAllActiveByServiceId($service_id)
{
$now = date('Y-m-d H:i:s');
$prototype = new CompanyService();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('service_id', $service_id);
$select->where->equalTo('status', CompanyService::ACTIVE);
$select->where->nest->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now );
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param int $company_id
* @param int $service_id
* @return CompanyService[]
*/
public function fetchOneByCompanyIdAndServiceId($company_id, $service_id)
{
$prototype = new CompanyService();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('company_id', $company_id);
$select->where->equalTo('service_id', $service_id);
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $company_id
* @param int $service_id
* @return CompanyService[]
*/
public function fetchOneActiveByCompanyIdAndServiceId($company_id, $service_id)
{
$now = date('Y-m-d H:i:s');
$prototype = new CompanyService();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('company_id', $company_id);
$select->where->equalTo('service_id', $service_id);
$select->where->equalTo('status', CompanyService::ACTIVE);
$select->where->nest->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now );
//echo $select->getSqlString($this->adapter->platform); exit;
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param CompanyService $companyService
* @return boolean
*/
public function insert($companyService)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($companyService);
$values = $this->removeEmpty($values);
if(!$companyService->paid_from) {
$values['paid_from'] = null;
}
if(!$companyService->paid_to) {
$values['paid_to'] = null;
}
$insert = $this->sql->insert(self::_TABLE);
$insert->values($values);
$result = $this->executeInsert($insert);
if($result) {
$companyService->id = $this->lastInsertId;
}
return $result;
}
/**
*
* @param CompanyService $companyService
* @return boolean
*/
public function update($companyService)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($companyService);
$values = $this->removeEmpty($values);
if(!$companyService->paid_from) {
$values['paid_from'] = null;
}
if(!$companyService->paid_to) {
$values['paid_to'] = null;
}
$update = $this->sql->update(self::_TABLE);
$update->set($values);
$update->where->equalTo('id', $companyService->id);
return $this->executeUpdate($update);
}
/**
*
* @param int $id
* @return boolean
*/
public function delete($id)
{
$delete = $this->sql->delete(self::_TABLE);
$delete->where->equalTo('id', $id);
return $this->executeDelete($delete);
}
/**
*
* @param int $company_id
* @param int $service_id
* @return boolean
*/
public function deleteByCompanyIdAndServiceId($company_id, $service_id)
{
$delete = $this->sql->delete(self::_TABLE);
$delete->where->equalTo('company_id', $company_id);
$delete->where->equalTo('service_id', $service_id);
return $this->executeDelete($delete);
}
}