|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\Controller\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
8
|
|
|
|
|
9
|
|
|
class Doctrine |
|
10
|
|
|
{ |
|
11
|
|
|
private $doctrine; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(ManagerRegistry $doctrine) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->doctrine = $doctrine; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function persist($object, $flush = false) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->getManager()->persist($object); |
|
21
|
|
|
|
|
22
|
|
|
if ($flush) { |
|
23
|
|
|
$this->flush($object); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function remove($object, $flush = false) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->getManager()->remove($object); |
|
30
|
|
|
|
|
31
|
|
|
if ($flush) { |
|
32
|
|
|
$this->flush(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function flush($object = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->getManager()->flush($object); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function findBy($object, $criterias = array()) |
|
42
|
|
|
{ |
|
43
|
|
|
$result = null; |
|
44
|
|
|
$findMethod = is_scalar($criterias) ? 'find' : 'findOneBy'; |
|
45
|
|
|
|
|
46
|
|
|
if (is_object($object) && $object instanceof ObjectRepository) { |
|
47
|
|
|
$result = $object->$findMethod($criterias); |
|
48
|
|
|
} elseif (is_object($object) && $this->getManager()->contains($object)) { |
|
49
|
|
|
$result = $this->getManager()->refresh($object); |
|
|
|
|
|
|
50
|
|
|
} elseif (is_string($object)) { |
|
51
|
|
|
$repository = $this->getRepository($object); |
|
52
|
|
|
$result = $repository->$findMethod($criterias); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (null !== $result) { |
|
56
|
|
|
return $result; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function findOr404($object, $criterias = array()) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($result = $this->findBy($object, $criterias)) { |
|
63
|
|
|
return $result; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
throw new NotFoundHttpException; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getManager() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->doctrine->getManager(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getRepository($object) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->getManager()->getRepository(is_object($object) ? get_class($object) : $object); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.