Skip to content

Commit

Permalink
Grid with custom repository method
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Mar 6, 2023
1 parent a2c507a commit b8d8c6c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Bundle/test/src/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use App\Entity\Book;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepositoryTrait;

Expand All @@ -26,4 +27,12 @@ public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Book::class);
}

public function createListQueryBuilder(): QueryBuilder
{
$queryBuilder = $this->createQueryBuilder('o');
$queryBuilder->andWhere($queryBuilder->expr()->isNotNull('o.archivedAt'));

return $queryBuilder;
}
}
6 changes: 5 additions & 1 deletion src/Bundle/test/src/Subscription/Entity/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace App\Subscription\Entity;

use App\Subscription\Repository\SubscriptionRepository;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Metadata\Create;
use Sylius\Component\Resource\Metadata\Delete;
Expand All @@ -35,9 +36,12 @@
#[Update]
#[Delete]
#[Show(template: 'subscription/show.html.twig')]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: SubscriptionRepository::class)]
class Subscription implements ResourceInterface
{
#[ORM\Column(type: 'datetime', nullable: true)]
public ?\DateTimeInterface $archivedAt = null;

public function __construct(
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/test/src/Subscription/Grid/SubscriptionGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function getName(): string
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->setRepositoryMethod('createListQueryBuilder', ['showArchived' => true])
->orderBy('email', 'asc')
->addField(
StringField::create('email')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Subscription\Repository;

use App\Subscription\Entity\Subscription;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepositoryTrait;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class SubscriptionRepository extends ServiceEntityRepository implements RepositoryInterface
{
use ResourceRepositoryTrait;

public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Subscription::class);
}

public function createListQueryBuilder(bool $showArchived = false): QueryBuilder
{
$queryBuilder = $this->createQueryBuilder('o');

if (!$showArchived) {
$queryBuilder->andWhere(
$queryBuilder->expr()->isNull('o.archivedAt'),
);
}

return $queryBuilder;
}
}
5 changes: 5 additions & 0 deletions src/Bundle/test/src/Tests/Controller/SubscriptionUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function it_allows_browsing_subscriptions(): void
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s">Show</a>', $subscriptions['subscription_doc']->getId()), $content);
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s/edit">Edit</a>', $subscriptions['subscription_doc']->getId()), $content);
$this->assertStringContainsString(sprintf('<form action="/admin/subscriptions/%s" method="post">', $subscriptions['subscription_doc']->getId()), $content);

$this->assertStringContainsString('<td>[email protected]</td>', $content);
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s">Show</a>', $subscriptions['subscription_archived']->getId()), $content);
$this->assertStringContainsString(sprintf('<a href="/admin/subscriptions/%s/edit">Edit</a>', $subscriptions['subscription_archived']->getId()), $content);
$this->assertStringContainsString(sprintf('<form action="/admin/subscriptions/%s" method="post">', $subscriptions['subscription_archived']->getId()), $content);
}

/** @test */
Expand Down
3 changes: 3 additions & 0 deletions src/Bundle/test/src/Tests/DataFixtures/ORM/subscriptions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ App\Subscription\Entity\Subscription:
email: '[email protected]'
subscription_doc:
email: '[email protected]'
subscription_archived:
email: '[email protected]'
archivedAt: '<dateTimeBetween("-200 days", "now")>'

0 comments on commit b8d8c6c

Please sign in to comment.