Rev 5775 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Mapper;
use LeadersLinked\Model\ContentReaction;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use Laminas\Db\Sql\Expression;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
class ContentReactionMapper extends MapperCommon
{
const _TABLE = 'tbl_content_reactions';
/**
*
* @var ContentReactionMapper
*/
private static $_instance;
/**
*
* @param AdapterInterface $adapter
*/
private function __construct($adapter)
{
parent::__construct($adapter);
}
/**
*
* @param AdapterInterface $adapter
* @return \LeadersLinked\Mapper\ContentReactionMapper
*/
public static function getInstance($adapter)
{
if(self::$_instance == null) {
self::$_instance = new ContentReactionMapper($adapter);
}
return self::$_instance;
}
/**
*
* @param int $feed_id
* @return array
*/
public function fetchCountContentReactionsByFeedId($feed_id)
{
$select = $this->sql->select(self::_TABLE);
$select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
$select->where->equalTo('feed_id', $feed_id);
$select->group('reaction');
return $this->executeFetchAllArray($select);
}
/**
*
* @param int $knowledge_area_id
* @return array
*/
public function fetchCountContentReactionsByKnowledgeAreaId($knowledge_area_id)
{
$select = $this->sql->select(self::_TABLE);
$select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
$select->where->equalTo('knowledge_area_id', $knowledge_area_id);
$select->group('reaction');
return $this->executeFetchAllArray($select);
}
/**
*
* @param int $post_id
* @return array
*/
public function fetchCountContentReactionsByPostId($post_id)
{
$select = $this->sql->select(self::_TABLE);
$select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
$select->where->equalTo('post_id', $post_id);
$select->group('reaction');
return $this->executeFetchAllArray($select);
}
/**
*
* @param int $feed_id
* @param int $user_id
* @return ContentReaction
*/
public function fetchOneByFeedIdAndUserId($feed_id, $user_id)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('feed_id', $feed_id);
$select->where->equalTo('user_id', $user_id);
$select->where->equalTo('relational', ContentReaction::RELATIONAL_FEED);
$prototype = new ContentReaction();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $post_id
* @param int $user_id
* @return ContentReaction
*/
public function fetchOneByPostIdAndUserId($post_id, $user_id)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('post_id', $post_id);
$select->where->equalTo('user_id', $user_id);
$select->where->equalTo('relational', ContentReaction::RELATIONAL_POST);
$prototype = new ContentReaction();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $knowledge_area_id
* @param int $user_id
* @return ContentReaction
*/
public function fetchOneByKnowledgeAreaIdAndUserId($knowledge_area_id, $user_id)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('knowledge_area_id', $knowledge_area_id);
$select->where->equalTo('user_id', $user_id);
$select->where->equalTo('relational', ContentReaction::RELATIONAL_KNOWLEDGE_AREA);
$prototype = new ContentReaction();
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $feed_id
* @param int $user_id
* @return boolean
*/
public function deleteByFeedIdAndUserId($feed_id, $user_id)
{
$delete = $this->sql->delete(self::_TABLE);
$delete->where->equalTo('feed_id', $feed_id);
$delete->where->equalTo('user_id', $user_id);
$delete->where->equalTo('relational', ContentReaction::RELATIONAL_FEED);
return $this->executeDelete($delete);
}
/**
*
* @param int $knowledge_area_id
* @param int $user_id
* @return boolean
*/
public function deleteByKnowledgeAreaIdAndUserId($knowledge_area_id, $user_id)
{
$delete = $this->sql->delete(self::_TABLE);
$delete->where->equalTo('knowledge_area_id', $knowledge_area_id);
$delete->where->equalTo('user_id', $user_id);
$delete->where->equalTo('relational', ContentReaction::RELATIONAL_KNOWLEDGE_AREA);
return $this->executeDelete($delete);
}
/**
*
* @param int $post_id
* @param int $user_id
* @return boolean
*/
public function deleteByPostIdAndUserId($post_id, $user_id)
{
$delete = $this->sql->delete(self::_TABLE);
$delete->where->equalTo('post_id', $post_id);
$delete->where->equalTo('user_id', $user_id);
$delete->where->equalTo('relational', ContentReaction::RELATIONAL_POST);
return $this->executeDelete($delete);
}
/**
*
* @param ContentReaction $contentReaction
* @return boolean
*/
public function insert($contentReaction)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($contentReaction);
$insert = $this->sql->insert(self::_TABLE);
$insert->values($values);
$response = $this->executeInsert($insert);
if($response) {
$contentReaction->id = $this->getLastInsertId();
}
return $response;
}
/**
*
* @param ContentReaction $contentReaction
* @return boolean
*/
public function update($contentReaction)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($contentReaction);
$update = $this->sql->update(self::_TABLE);
$update->set($values);
$update->where->equalTo('id', $contentReaction->id);
return $this->executeUpdate($update);
}
}