Answer the question
In order to leave comments, you need to log in
Interaction of several repositories. How to merge?
Hello.
I have something like this in my system:
shop/
--entity/
------Product.php
------ProductType.php
------Category.php
--repository/
------ProductRepository.php
---- --CategoryRepository.php
------ProductTypeRepository.php
.... and more....
interface CategoryRepositoryInterface
{
public function findOne($id): CategoryInterface;
public function findAll(): array;
public function save(CategoryInterface $category);
public function remove(CategoryInterface $category);
}
interface ProductRepository
{
// .... все тоже самое, что и у CategoryRepositoryInterface
}
interface Product
{
public function addCategory(CategoryInterface $category);
public function setProductType(ProductTypeInterface $productTypeInterface);
}
$productRepository->findOne(100); // вжух и мы получили товар со всеми зависимостями и ИД 100.
Answer the question
In order to leave comments, you need to log in
It turns out that ProductRepository depends on other repositories (Category, ProductType, Image, File....).
This is how you do it. It may or may not depend. I would not tie one to the other. Let there be a similar code ... that's okay.
Hands.
Individually. Hands.
Or take ORM or, again, by hand. Do not be afraid to write code and do not be afraid of duplicating it within reasonable limits. Not everything is DRY what they think.
Maxim Fedorov wrote you correctly, you need another layer that will interact with your repositories, in symphony, as in any other framework, all logic should be in the service. From your description, you have made a facade pattern so that everything is simple in the client code. For this, a service is suitable, which can also inherit (implement) the interface.
interface CategoryRepositoryInterface
{
public function findOne($id);
public function findAll(): array;
public function save(CategoryInterface $category);
public function remove(CategoryInterface $category);
}
class CustomNameOfClass implements CategoryRepositoryInterface // ну тут имя лучше изменить
{
public function findOne($id){}; // тут тоже лучше иметь return type
public function findAll(){}: array;
public function save(CategoryInterface $category){}; // тут тоже лучше иметь return type
public function remove(CategoryInterface $category){}; // тут тоже лучше иметь return type
}
I don't know how good it is architecturally. Alternatively, you can add getter methods or modifier methods to the Product entity that will pull the necessary repositories.
$product = $productRepository->findOne(100);
$product->getCategories(); // Здесь по принципу lazy load ищем в нужном репозитории
$product->removeCategories();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question