Skip to content

Commit

Permalink
FurnitureDataProvider using collectionDataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
aratinau committed Jul 5, 2021
1 parent 6bfa778 commit 2cab86c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Inspired from https://github.com/api-platform/demo

There is five examples in this repo
There is six examples in this repo

## First Example use raw data from a csv file

Expand Down Expand Up @@ -74,6 +74,18 @@ The pagination is available

This example show how use PaginationExtension in JobCollectionDataProvider

### Usage

`api/jobs?page=1`

## Sixth example use FurnitureDataProvider (collectionDataProvider)

Basic example showing how use and configure CollectionDataProvider

### Usage

`api/furniture?page=2`

## Install

composer install
Expand Down
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ services:
App\DataProvider\CarCollectionDataProvider:
bind:
$collectionExtensions: !tagged api_platform.doctrine.orm.query_extension.collection

App\DataProvider\FurnitureDataProvider:
bind:
$collectionDataProvider: '@api_platform.doctrine.orm.default.collection_data_provider'
4 changes: 4 additions & 0 deletions fixtures/furniture.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
App\Entity\Furniture:
furniture_{1..1000}:
name: <word(10, 700)>
isPublished: '<numberBetween(0, 1)>'
28 changes: 28 additions & 0 deletions src/DataProvider/FurnitureDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\DataProvider;

use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\Furniture;

class FurnitureDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $collectionDataProvider;

public function __construct(CollectionDataProviderInterface $collectionDataProvider)
{
$this->collectionDataProvider = $collectionDataProvider;
}

public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return $resourceClass === Furniture::class;
}

public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
{
return $this->collectionDataProvider->getCollection($resourceClass, $operationName, $context);
}
}
60 changes: 60 additions & 0 deletions src/Entity/Furniture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\FurnitureRepository;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;

/**
* @ApiResource()
* @ORM\Entity(repositoryClass=FurnitureRepository::class)
*/
class Furniture
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=255)
*/
private $name;

/**
* @ORM\Column(type="boolean")
*/
private $isPublished;

public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getIsPublished(): ?bool
{
return $this->isPublished;
}

public function setIsPublished(bool $isPublished): self
{
$this->isPublished = $isPublished;

return $this;
}
}
50 changes: 50 additions & 0 deletions src/Repository/FurnitureRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Repository;

use App\Entity\Furniture;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @method Furniture|null find($id, $lockMode = null, $lockVersion = null)
* @method Furniture|null findOneBy(array $criteria, array $orderBy = null)
* @method Furniture[] findAll()
* @method Furniture[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class FurnitureRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Furniture::class);
}

// /**
// * @return Furniture[] Returns an array of Furniture objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('f')
->andWhere('f.exampleField = :val')
->setParameter('val', $value)
->orderBy('f.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/

/*
public function findOneBySomeField($value): ?Furniture
{
return $this->createQueryBuilder('f')
->andWhere('f.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

0 comments on commit 2cab86c

Please sign in to comment.