Rev 5319 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Mapper;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Model\ChatUser;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
class ChatUserMapper extends MapperCommon
{
const _TABLE = 'tbl_chat_users';
/**
*
* @var ChatUserMapper
*/
private static $_instance;
/**
*
* @param AdapterInterface $adapter
* @param int $user_id
*/
private function __construct($adapter)
{
parent::__construct($adapter);
}
/**
*
* @param AdapterInterface $adapter
* @return ChatUserMapper
*/
public static function getInstance($adapter)
{
if(self::$_instance == null) {
self::$_instance = new ChatUserMapper($adapter);
}
return self::$_instance;
}
/**
*
* @param int $id
* @return ChatUser
*/
public function fetchOne($id)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('id', $id);
$prototype = new ChatUser();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param string $uuid
* @return ChatUser
*/
public function fetchOneByUuid($uuid)
{
$prototype = new ChatUser();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('uuid', $uuid);
$select->limit(1);
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $user1
* @param int $user2
* @return ChatUser
*/
public function fetchOneByUserId1AndUserId2($user1, $user2)
{
$select = $this->sql->select(self::_TABLE);
$select->where->nest()->equalTo('user_id1', $user1)->and->equalTo('user_id2', $user2)->unnest()
->or->nest()->equalTo('user_id1', $user2)->and->equalTo('user_id2', $user1)->unnest();
//echo $select->getSqlString($this->adapter->platform); exit;
$prototype = new ChatUser();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $user_id
* @return ChatUser[]
*/
public function fetchAllByUserId($user_id)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('user_id1', $user_id)->or->equalTo('user_id2', $user_id);
//echo $select->getSqlString($this->adapter->platform); exit;
$prototype = new ChatUser();
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param int $user_id
* @return ChatUser[]
*/
public function fetchAllByUserIdOnlyOpen($user_id)
{
$select = $this->sql->select(self::_TABLE);
$select->where->nest()
->equalTo('user_id1', $user_id)
->and->equalTo('user_open1', ChatUser::OPEN_YES)
->unnest()->or->nest()
->equalTo('user_id2', $user_id)
->and->equalTo('user_open2', ChatUser::OPEN_YES)
->unnest();
//echo $select->getSqlString($this->adapter->platform); exit;
$prototype = new ChatUser();
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param ChatUser $chatUser
* @return boolean
*/
public function insert($chatUser)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($chatUser);
$values = $this->removeEmpty($values);
$insert = $this->sql->insert(self::_TABLE);
$insert->values($values);
//echo $insert->getSqlString($this->adapter->platform); exit;
$result = $this->executeInsert($insert);
if($result) {
$chatUser->id = $this->lastInsertId;
}
return $result;
}
/**
*
* @param int $id
* @return boolean
*/
public function markIsOpen1($id)
{
$update = $this->sql->update(self::_TABLE);
$update->set(['user_open1' => ChatUser::OPEN_YES]);
$update->where->equalTo('id', $id);
return $this->executeUpdate($update);
}
/**
*
* @param int $id
* @return boolean
*/
public function markIsOpen2($id)
{
$update = $this->sql->update(self::_TABLE);
$update->set(['user_open2' => ChatUser::OPEN_YES]);
$update->where->equalTo('id', $id);
return $this->executeUpdate($update);
}
/**
*
* @param int $id
* @return boolean
*/
public function markIsClose1($id)
{
$update = $this->sql->update(self::_TABLE);
$update->set(['user_open1' => ChatUser::OPEN_NO]);
$update->where->equalTo('id', $id);
//echo $update->getSqlString($this->adapter->platform); exit;
return $this->executeUpdate($update);
}
/**
*
* @param int $id
* @return boolean
*/
public function markIsClose2($id)
{
$update = $this->sql->update(self::_TABLE);
$update->set(['user_open2' => ChatUser::OPEN_NO]);
$update->where->equalTo('id', $id);
//echo $update->getSqlString($this->adapter->platform); exit;
return $this->executeUpdate($update);
}
}