Last active
February 17, 2023 14:53
-
-
Save WalterWoshid/bb8259ffc6eda486aedf7bba760f3203 to your computer and use it in GitHub Desktop.
Magento 2: Many-to-Many-Relationship - CRUD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace WalterWoshid\CustomBlog\Api\Data; | |
use Magento\Framework\Api\ExtensibleDataInterface; | |
/** | |
* Interface ProductBlogRelationInterface | |
* | |
* @package WalterWoshid\CustomBlog\Api\Data | |
* @api | |
*/ | |
interface ProductBlogRelationInterface extends ExtensibleDataInterface | |
{ | |
/**#@+ | |
* Constants for keys of data array. Identical to the name of the getter in snake case | |
*/ | |
const RELATION_ID = 'relation_id'; | |
const PRODUCT_ID = 'product_id'; | |
const BLOG_ID = 'blog_id'; | |
/**#@-*/ | |
/** | |
* Get relation ID | |
* | |
* @return mixed | |
*/ | |
public function getId(); | |
/** | |
* Set relation ID | |
* | |
* @param $value | |
* @return $this | |
*/ | |
public function setId($value): self; | |
/** | |
* Get product ID | |
* | |
* @return int | |
*/ | |
public function getProductId(): int; | |
/** | |
* Set product ID | |
* | |
* @param int $productId | |
* @return $this | |
*/ | |
public function setProductId(int $productId): self; | |
/** | |
* Get blog ID | |
* | |
* @return int | |
*/ | |
public function getBlogId(): int; | |
/** | |
* Set blog ID | |
* | |
* @param int $blogId | |
* @return $this | |
*/ | |
public function setBlogId(int $blogId): self; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
... | |
<!-- Product - Blog Relation --> | |
<preference for="WalterWoshid\CustomBlog\Api\Data\ProductBlogRelationInterface" | |
type="WalterWoshid\CustomBlog\Model\ProductBlogRelation"/> | |
... | |
</config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace WalterWoshid\CustomBlog\Model; | |
use Magento\Framework\Model\AbstractModel; | |
use WalterWoshid\CustomBlog\Api\Data\ProductBlogRelationInterface; | |
use WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation as Resource; | |
/** | |
* Class ProductBlogRelation | |
* | |
* @package WalterWoshid\CustomBlog\Model | |
* @api | |
*/ | |
class ProductBlogRelation extends AbstractModel implements ProductBlogRelationInterface | |
{ | |
/** | |
* Custom blog product relation cache tag | |
*/ | |
const CACHE_TAG = 'custom_blog_prod_blog_rel'; | |
protected $_cacheTag = self::CACHE_TAG; | |
protected $_eventPrefix = 'custom_blog_product_blog_relation'; | |
protected function _construct() | |
{ | |
$this->_init(Resource::class); | |
} | |
public function getId() | |
{ | |
return $this->getData(self::RELATION_ID); | |
} | |
public function setId($value): ProductBlogRelationInterface | |
{ | |
return $this->setData(self::RELATION_ID, $value); | |
} | |
public function getProductId(): int | |
{ | |
return $this->getData(self::PRODUCT_ID); | |
} | |
public function setProductId(int $productId): ProductBlogRelationInterface | |
{ | |
return $this->setData(self::PRODUCT_ID, $productId); | |
} | |
public function getBlogId(): int | |
{ | |
return $this->getData(self::BLOG_ID); | |
} | |
public function setBlogId(int $blogId): ProductBlogRelationInterface | |
{ | |
return $this->setData(self::BLOG_ID, $blogId); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace WalterWoshid\CustomBlog\Model\ResourceModel; | |
use Magento\Framework\Model\ResourceModel\Db\AbstractDb; | |
use WalterWoshid\CustomBlog\Api\Data\ProductBlogRelationInterface; | |
/** | |
* Class ProductBlogRelation | |
* | |
* @package WalterWoshid\CustomBlog\Model\ResourceModel | |
* @api | |
*/ | |
class ProductBlogRelation extends AbstractDb | |
{ | |
const TABLE_NAME = 'custom_blog_product_blog_relation'; | |
protected function _construct() | |
{ | |
$this->_init(self::TABLE_NAME, ProductBlogRelationInterface::RELATION_ID); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation; | |
use Magento\Cms\Model\ResourceModel\AbstractCollection; | |
use WalterWoshid\CustomBlog\Model\ProductBlogRelation as ProductBlogRelationModel; | |
use WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation as ProductBlogRelationResource; | |
/** | |
* Class Collection | |
* | |
* @package WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation | |
* @api | |
*/ | |
class Collection extends AbstractCollection | |
{ | |
public function _construct() | |
{ | |
$this->_init( | |
ProductBlogRelationModel::class, | |
ProductBlogRelationResource::class | |
); | |
} | |
public function addStoreFilter($store, $withAdmin = true): self | |
{ | |
if (!$this->getFlag('store_filter_added')) { | |
$this->performAddStoreFilter($store, $withAdmin); | |
$this->setFlag('store_filter_added', true); | |
} | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment