Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WalterWoshid/e83b83bfdbe66fdf59f19ed22a13c696 to your computer and use it in GitHub Desktop.
Save WalterWoshid/e83b83bfdbe66fdf59f19ed22a13c696 to your computer and use it in GitHub Desktop.
Magento 2: Many-to-Many-Relationship - Repository

Magento 2 - Many-to-Many-Relationship

Repository

Twitter: @WalterWoshid

<?php
namespace WalterWoshid\CustomBlog\Api;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use WalterWoshid\CustomBlog\Api\Data\ProductBlogRelationInterface;
use WalterWoshid\CustomBlog\Model\ProductBlogRelation as ProductBlogRelationModel;
use WalterWoshid\CustomBlog\Model\ResourceModel\Blog\Collection as BlogCollection;
use WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation\Collection as ProductBlogRelationCollection;
/**
* Interface ProductBlogRelationRepositoryInterface
*
* @package WalterWoshid\CustomBlog
* @api
*/
interface ProductBlogRelationRepositoryInterface
{
/**
* Creates a new empty model
*
* @return ProductBlogRelationModel
*/
public function newModel(): ProductBlogRelationModel;
/**
* Creates a new empty collection
*
* @return ProductBlogRelationCollection
*/
public function newCollection(): ProductBlogRelationCollection;
/**
* Create new relation
*
* @param int $blogId
* @param int $productId
* @return ProductBlogRelationModel
* @throws CouldNotSaveException
*/
public function create(int $blogId, int $productId): ProductBlogRelationModel;
/**
* Save relation
*
* @param ProductBlogRelationInterface $relation
* @return ProductBlogRelationInterface
* @throws CouldNotSaveException
*/
public function save(ProductBlogRelationInterface $relation): ProductBlogRelationInterface;
/**
* Retrieve relations by product ID
*
* @param int $productId
* @return ProductBlogRelationCollection
*/
public function getByProductId(int $productId): ProductBlogRelationCollection;
/**
* Retrieve relations by blog ID
*
* @param int $blogId
* @return ProductBlogRelationCollection
*/
public function getByBlogId(int $blogId): ProductBlogRelationCollection;
/**
* Delete relation
*
* @param ProductBlogRelationInterface $relation
* @return bool
* @throws CouldNotDeleteException
*/
public function delete(ProductBlogRelationInterface $relation): bool;
/**
* Delete relations by product id
*
* @param int $productId
* @return bool
* @throws CouldNotDeleteException
*/
public function deleteByProductId(int $productId): bool;
/**
* Delete relations by id
*
* @param int $blogId
* @param int $productId
* @return bool
* @throws CouldNotDeleteException
*/
public function deleteById(int $blogId, int $productId): bool;
/**
* Delete relations by blog ids with optional product id
*
* @param int[] $blogIds
* @param int|null $productId
* @return bool
* @throws CouldNotDeleteException
*/
public function deleteByBlogIds(array $blogIds, ?int $productId = null): bool;
/**
* Get related blogs by product id
*
* @param int $productId
* @return BlogCollection
*/
public function getRelatedBlogsByProductId(int $productId): BlogCollection;
/**
* Get related blog ids by product id
*
* @param int $productId
* @return int[]
*/
public function getRelatedBlogIdsByProductId(int $productId): array;
}
<?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\ProductBlogRelationRepositoryInterface"
type="WalterWoshid\CustomBlog\Model\ProductBlogRelationRepository"/>
...
</config>
<?php
namespace WalterWoshid\CustomBlog\Model;
use Exception;
use Magento\Framework\App\Config\BaseFactory;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use WalterWoshid\CustomBlog\Api\Data\ProductBlogRelationInterface;
use WalterWoshid\CustomBlog\Api\ProductBlogRelationRepositoryInterface;
use WalterWoshid\CustomBlog\Model\Blog as BlogModel;
use WalterWoshid\CustomBlog\Model\ProductBlogRelation as ProductBlogRelationModel;
use WalterWoshid\CustomBlog\Model\ProductBlogRelationFactory as ProductBlogRelationModelFactory;
use WalterWoshid\CustomBlog\Model\ResourceModel\Blog\Collection as BlogCollection;
use WalterWoshid\CustomBlog\Model\ResourceModel\Blog\CollectionFactory as BlogCollectionFactory;
use WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation as ProductBlogRelationResource;
use WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation\Collection as ProductBlogRelationCollection;
use WalterWoshid\CustomBlog\Model\ResourceModel\ProductBlogRelation\CollectionFactory as ProductBlogRelationCollectionFactory;
/**
* Class ProductBlogRelationRepository
*
* @package WalterWoshid\CustomBlog\Model
* @api
*/
class ProductBlogRelationRepository implements ProductBlogRelationRepositoryInterface
{
/**
* @var ProductBlogRelationResource
*/
protected ProductBlogRelationResource $resource;
/**
* @var ProductBlogRelationModelFactory|BaseFactory
*/
private ProductBlogRelationModelFactory $productBlogRelationModelFactory;
/**
* @var ProductBlogRelationCollectionFactory|BaseFactory
*/
private ProductBlogRelationCollectionFactory $productBlogRelationCollectionFactory;
/**
* @var BlogCollectionFactory
*/
private BlogCollectionFactory $blogCollectionFactory;
/**
* ProductBlogRelationRepository constructor
*
* @param ProductBlogRelationResource $resource
* @param ProductBlogRelationModelFactory $productBlogRelationModelFactory
* @param ProductBlogRelationCollectionFactory $productBlogRelationCollectionFactory
* @param BlogCollectionFactory $blogCollectionFactory
*/
public function __construct(
ProductBlogRelationResource $resource,
ProductBlogRelationModelFactory $productBlogRelationModelFactory,
ProductBlogRelationCollectionFactory $productBlogRelationCollectionFactory,
BlogCollectionFactory $blogCollectionFactory
) {
$this->resource = $resource;
$this->productBlogRelationModelFactory = $productBlogRelationModelFactory;
$this->productBlogRelationCollectionFactory = $productBlogRelationCollectionFactory;
$this->blogCollectionFactory = $blogCollectionFactory;
}
public function newModel(): ProductBlogRelationModel
{
return $this->productBlogRelationModelFactory->create();
}
public function newCollection(): ProductBlogRelationCollection
{
return $this->productBlogRelationCollectionFactory->create();
}
public function create(int $blogId, int $productId): ProductBlogRelationModel
{
$productBlogRelationModel = $this->newModel()
->setBlogId($blogId)
->setProductId($productId);
$this->save($productBlogRelationModel);
return $productBlogRelationModel;
}
public function save(ProductBlogRelationInterface $relation): ProductBlogRelationInterface
{
try {
$this->resource->save($relation);
} catch (Exception $exception) {
throw new CouldNotSaveException(__($exception->getMessage()));
}
return $relation;
}
public function getByProductId(int $productId): ProductBlogRelationCollection
{
return $this->newCollection()
->addFieldToFilter(
ProductBlogRelationInterface::PRODUCT_ID,
$productId
);
}
public function getByBlogId(int $blogId): ProductBlogRelationCollection
{
return $this->newCollection()
->addFieldToFilter(
ProductBlogRelationInterface::BLOG_ID,
$blogId
);
}
public function delete(ProductBlogRelationInterface $relation): bool
{
try {
$this->resource->delete($relation);
} catch (Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}
return true;
}
public function deleteByProductId(int $productId): bool
{
try {
$this->newCollection()
->addFieldToFilter(ProductBlogRelationInterface::PRODUCT_ID, $productId)
->walk('delete');
return true;
} catch (Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}
}
public function deleteById(int $blogId, int $productId): bool
{
try {
$this->newCollection()
->addFieldToFilter(ProductBlogRelationInterface::BLOG_ID, $blogId)
->addFieldToFilter(ProductBlogRelationInterface::PRODUCT_ID, $productId)
->walk('delete');
return true;
} catch (Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}
}
public function deleteByBlogIds(array $blogIds, ?int $productId = null): bool
{
try {
$collection = $this->newCollection()
->addFieldToFilter(ProductBlogRelationInterface::BLOG_ID, $blogIds);
if ($productId) {
$collection->addFieldToFilter(ProductBlogRelationInterface::PRODUCT_ID, $productId);
}
$collection->walk('delete');
return true;
} catch (Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}
}
public function getRelatedBlogsByProductId(int $productId): BlogCollection
{
/** @var BlogCollection $collection */
$collection = $this->blogCollectionFactory->create();
$tableName = $collection->getTable(ProductBlogRelationResource::TABLE_NAME);
$collection
->addFieldToFilter(
'product_blog_relation.product_id',
$productId
)
->getSelect()
->joinLeft(
['product_blog_relation' => $tableName],
'main_table.blog_id = product_blog_relation.blog_id'
);
return $collection;
}
public function getRelatedBlogIdsByProductId(int $productId): array
{
$collection = $this->getRelatedBlogsByProductId($productId);
$blogIds = [];
foreach ($collection->getItems() as $blog) {
/** @var BlogModel $blog */
$blogIds[] = $blog->getId();
}
return $blogIds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment