-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryController.php
112 lines (90 loc) · 3.32 KB
/
RepositoryController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace App\Controller;
use App\Repository\ProductRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class RepositoryController
* @package App\Controller
* @Route("/repository")
*/
class RepositoryController extends AbstractController
{
// https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/query-builder.html
// https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/dql-doctrine-query-language.html
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/", name="qb")
*/
public function index()
{
return $this->json([]);
}
/**
* @Route("/find-one/{id}", name="repository_find_one")
*/
public function find(ProductRepository $productRepository, $id = 1)
{
// We use the repository
$product = $productRepository->find($id);
return $this->json($product, 200, [], ['groups' => 'products:read']);
}
/**
* @Route("/find-all", name="repository_find_all")
*/
public function findAll(ProductRepository $productRepository)
{
// We use the repository
$products = $productRepository->findAll();
return $this->json($products, 200, [], ['groups' => 'products:read']);
}
/**
* @Route("/find-one-by/{attribute}/{value}", name="repository_find_one_by")
*/
public function findOneBy(ProductRepository $productRepository, string $attribute = 'id', string $value = '1')
{
// We use the repository
$product = $productRepository->findOneBy([$attribute => $value]);
// Important => we can set many criteria
// example $productRepository->findOneBy(['productCategory' => 3, 'isAvailable' => true])
return $this->json($product, 200, [], ['groups' => 'products:read']);
}
/**
* @Route("/find-by/{attribute}/{value}", name="repository_find_by")
*/
public function findBy(ProductRepository $productRepository, string $attribute = 'id', string $value = '1')
{
// We use the repository
$product = $productRepository->findBy([$attribute => $value]);
// Important => we can set many criteria
// $productRepository->findBy(['productCategory' => 3, 'isAvailable' => true])
$data = [
'Important' => 'We can set many criteria: $productRepository->findBy([\'productCategory\' => 3, \'isAvailable\' => true])',
'query' => "\$productRepository->findBy([$attribute => $value])",
'query result' => $product,
];
return $this->json($data, 200, [], ['groups' => 'products:read']);
}
/**
* @Route("/count-result", name="repository_count_result")
* @param UserRepository $userRepository
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function selectSome(UserRepository $userRepository)
{
$result = $userRepository->findAll();
return $this->json([
'count' => 'count($result)',
'result' => count($result)
]);
}
}