1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repository; |
4
|
|
|
|
5
|
|
|
use App\Entity\Product; |
6
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
7
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @extends ServiceEntityRepository<Product> |
11
|
|
|
*/ |
12
|
|
|
class ProductRepository extends ServiceEntityRepository |
13
|
|
|
{ |
14
|
|
|
public function __construct(ManagerRegistry $registry) |
15
|
|
|
{ |
16
|
|
|
parent::__construct($registry, Product::class); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Find all producs having a value above the specified one. |
21
|
|
|
* |
22
|
|
|
* @return Product[] Returns an array of Product objects |
23
|
|
|
*/ |
24
|
|
|
public function findByMinimumValue(int $value): array |
25
|
|
|
{ |
26
|
|
|
/** @var Product[] $result */ |
27
|
|
|
$result = $this->createQueryBuilder('p') |
28
|
|
|
->andWhere('p.value >= :value') |
29
|
|
|
->setParameter('value', $value) |
30
|
|
|
->orderBy('p.value', 'ASC') |
31
|
|
|
->getQuery() |
32
|
|
|
->getResult() |
33
|
|
|
; |
34
|
|
|
|
35
|
|
|
return $result; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Find all producs having a value above the specified one with SQL. |
40
|
|
|
* |
41
|
|
|
* @return array<int, array<string, mixed>> Returns an array of arrays (i.e. a raw data set) |
42
|
|
|
*/ |
43
|
|
|
public function findByMinimumValue2(int $value): array |
44
|
|
|
{ |
45
|
|
|
$conn = $this->getEntityManager()->getConnection(); |
46
|
|
|
|
47
|
|
|
$sql = ' |
48
|
|
|
SELECT * FROM product AS p |
49
|
|
|
WHERE p.value >= :value |
50
|
|
|
ORDER BY p.value ASC |
51
|
|
|
'; |
52
|
|
|
|
53
|
|
|
$resultSet = $conn->executeQuery($sql, ['value' => $value]); |
54
|
|
|
|
55
|
|
|
return $resultSet->fetchAllAssociative(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// /** |
59
|
|
|
// * @return Product[] Returns an array of Product objects |
60
|
|
|
// */ |
61
|
|
|
// public function findByExampleField($value): array |
62
|
|
|
// { |
63
|
|
|
// return $this->createQueryBuilder('p') |
64
|
|
|
// ->andWhere('p.exampleField = :val') |
65
|
|
|
// ->setParameter('val', $value) |
66
|
|
|
// ->orderBy('p.id', 'ASC') |
67
|
|
|
// ->setMaxResults(10) |
68
|
|
|
// ->getQuery() |
69
|
|
|
// ->getResult() |
70
|
|
|
// ; |
71
|
|
|
// } |
72
|
|
|
|
73
|
|
|
// public function findOneBySomeField($value): ?Product |
74
|
|
|
// { |
75
|
|
|
// return $this->createQueryBuilder('p') |
76
|
|
|
// ->andWhere('p.exampleField = :val') |
77
|
|
|
// ->setParameter('val', $value) |
78
|
|
|
// ->getQuery() |
79
|
|
|
// ->getOneOrNullResult() |
80
|
|
|
// ; |
81
|
|
|
// } |
82
|
|
|
} |
83
|
|
|
|