catalogResource = $catalogResource; $this->catalogCollection = $catalogCollection; $this->dateTime = $dateTime; parent::__construct( $context, $registry, $resource, $resourceCollection, $data ); } /** * Constructor. * * @return null */ public function _construct() { $this->_init(\Dotdigitalgroup\Email\Model\ResourceModel\Catalog::class); } /** * Prepare data to be saved to database. * * @return $this */ public function beforeSave() { parent::beforeSave(); if ($this->isObjectNew() && !$this->getCreatedAt()) { $this->setCreatedAt($this->dateTime->formatDate(true)); } $this->setUpdatedAt($this->dateTime->formatDate(true)); return $this; } /** * Load by product id. * * @param int $productId * * @return \Dotdigitalgroup\Email\Model\Catalog */ public function loadProductById($productId) { $collection = $this->catalogCollection->create() ->addFieldToFilter('product_id', $productId) ->setPageSize(1); return $collection->getFirstItem(); } }