Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
AlexaLog.php 2.47 KiB
<?php
namespace NicolasBejean\Alexa\Model\ResourceModel;

use \NicolasBejean\Alexa\Api\Data\AlexaLogInterface;
use \NicolasBejean\Alexa\Model\AlexaLog as AlexaLogModel;
use \Magento\Framework\EntityManager\EntityManager;
use \Magento\Framework\EntityManager\MetadataPool;
use \Magento\Framework\Model\AbstractModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use \Magento\Framework\Model\ResourceModel\Db\Context;
use \Exception;

class AlexaLog extends AbstractDb
{
    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @var MetadataPool
     */
    protected $metadataPool;

    /**
     * @param Context $context
     * @param EntityManager $entityManager
     * @param MetadataPool $metadataPool
     * @param string $connectionName
     */
    public function __construct(
        Context $context,
        EntityManager $entityManager,
        MetadataPool $metadataPool,
        $connectionName = null
    ) {
        $this->entityManager = $entityManager;
        $this->metadataPool = $metadataPool;
        parent::__construct($context, $connectionName);
    }

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('nicolasbejean_alexa_log', 'entity_id');
    }

    /**
     * @inheritDoc
     */
    public function getConnection()
    {
        try {
            return $this->metadataPool->getMetadata(AlexaLogInterface::class)->getEntityConnection();
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * Perform operations before object save
     *
     * @param AbstractModel $object
     * @return $this
     */
    protected function _beforeSave(AbstractModel $object)
    {
        return $this;
    }

    /**
     * Load an object
     *
     * @param AlexaLogModel|AbstractModel $object
     * @param int $id
     * @param null $field
     * @return $this
     */
    public function load(AbstractModel $object, $id, $field = null)
    {
        return $this->entityManager->load($object, $id);
    }

    /**
     * Save an object.
     *
     * @param AbstractModel $object
     * @return $this
     * @throws Exception
     */
    public function save(AbstractModel $object)
    {
        $this->entityManager->save($object);
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function delete(AbstractModel $object)
    {
        $this->entityManager->delete($object);
        return $this;
    }
}