Skip to content

Commit

Permalink
Update docs with repositories customizing
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Apr 23, 2019
1 parent 8439891 commit 9142276
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions docs/customization/repository.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 +58,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 +79,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**.
You can now use your method in anywhere when you are operating on the Product repository.
For example you can configure new route:

.. code-block:: php
<?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 9142276

Please sign in to comment.