Nextcloud-App/lib/Db/ChatMapper.php

64 lines
1.7 KiB
PHP

<?php
namespace OCA\UPschooling\Db;
use Doctrine\DBAL\Types\Type;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class ChatMapper extends QBMapper
{
public function __construct(IDBConnection $db)
{
parent::__construct($db, 'upschooling_chats', MatrixChat::class);
}
/**
* @param int $ticketId
* @return array array of Entity|MatrixChat
*/
public function findForTicket(int $ticketId): array
{
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('upschooling_chats')
->where($qb->expr()->eq('ticket_id', $qb->createNamedParameter($ticketId, IQueryBuilder::PARAM_INT)));
return $this->findEntities($qb);
}
/**
* @param int $ticketId
* @return Entity|MatrixChat
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
public function findCurrent(int $ticketId): MatrixChat
{
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('upschooling_chats')
->where(
$qb->expr()->eq('ticket_id', $qb->createNamedParameter($ticketId, IQueryBuilder::PARAM_INT)),
$qb->expr()->isNull('date_end')
);
return $this->findEntity($qb);
}
/**
* @param \DateTime $dateTime
* @return mixed The database representation of the datetime with timezone.
* @throws \Doctrine\DBAL\Exception
* @throws \Doctrine\DBAL\Types\ConversionException
*/
public function convertDateTimeTz(\DateTime $dateTime)
{
$dateTimeTzType = Type::getType("datetimetz");
return $dateTimeTzType->convertToDatabaseValue($dateTime, $this->db->getDatabasePlatform());
}
}