Skip to content

Commit

Permalink
bug #10290 [Docs] Update "Customizing Repositories" (AdamKasp)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.3 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.3
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| License         | MIT



Commits
-------

70c8d5c Update docs with repositories customizing
  • Loading branch information
GSadee authored May 9, 2019
2 parents a41a944 + 70c8d5c commit 043a672
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions docs/customization/repository.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ You may need for instance:
How to customize a Repository?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. tip::

You can browse the full implementation of this example on `this GitHub Pull Request.
<https://github.com/Sylius/Customizations/pull/5>`__

Let's assume that you would want to find products that you are running out of in the inventory.

**1.** Create your own repository class under the ``App\Repository`` namespace.
Expand All @@ -30,6 +35,23 @@ For the ``ProductRepository`` run:
$ php bin/console debug:container sylius.repository.product
As a result you will get the ``Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository`` - this is the class that you need to be extending.
To make your class more reusable, you should create a new interface ``src/Repository/ProductRepositoryInterface.php``
which will extend ``Sylius\Component\Core\Repository\ProductRepositoryInterface``

.. code-block:: php
<?php
declare(strict_types=1);
namespace App\Repository;
use Sylius\Component\Core\Repository\ProductRepositoryInterface as BaseProductRepositoryInterface;
interface ProductRepositoryInterface extends BaseProductRepositoryInterface
{
public function findAllByOnHand(int $limit): array;
}
.. code-block:: php
Expand All @@ -41,12 +63,7 @@ As a result you will get the ``Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepo
class ProductRepository extends BaseProductRepository
{
/**
* @param int $limit
*
* @return array
*/
public function findByOnHand(int $limit = 8): array
public function findAllByOnHand(int $limit = 8): array
{
return $this->createQueryBuilder('o')
->addSelect('variant')
Expand All @@ -67,29 +84,38 @@ As we are selecting Products we need to have a join to translations, because the
We are sorting the results by the count of how many products are still available on hand, which is saved on the ``onHand`` field on the specific ``variant`` of each product.
Then we are limiting the query to 8 by default, to get only 8 products that are low in stock.

**2.** In order to use your repository you need to configure it in the ``config/_sylius.yaml``.
**2.** In order to use your repository you need to configure it in the ``config/packages/_sylius.yaml``.
As you can see in the ``_sylius.yaml`` you already have a basic configuration, now you just need to add your repository and override resourceRepository

.. code-block:: yaml
sylius_product:
resources:
product:
classes:
...
repository: App\Repository\ProductRepository
...
**3.** After configuring the ``sylius.repository.product`` service has your ``findByOnHand()`` method available.
You can form now on use your method in any **Controller**.

.. code-block:: php
You can now use your method in anywhere when you are operating on the Product repository.
For example you can configure new route:

<?php
public function lowInStockAction()
{
$productRepository = $this->container->get('sylius.repository.product');
.. code-block:: yaml
$lowInStock = $productRepository->findByOnHand();
}
app_shop_partial_product_index_by_on_hand:
path: /partial/products/by-on-hand
methods: [GET]
defaults:
_controller: sylius.controller.product:indexAction
_sylius:
template: '@SyliusShop/Product/_horizontalList.html.twig'
repository:
method: findAllByOnHand
arguments: [4]
criteria: false
paginate: false
limit: 100
What happens while overriding Repositories?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 043a672

Please sign in to comment.