|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pim\Bundle\CustomEntityBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Akeneo\Bundle\StorageUtilsBundle\Doctrine\SmartManagerRegistry; |
|
6
|
|
|
use Doctrine\Common\Util\ClassUtils; |
|
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base implementation for ORM managers |
|
11
|
|
|
* |
|
12
|
|
|
* @author Antoine Guigan <[email protected]> |
|
13
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
|
14
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
15
|
|
|
*/ |
|
16
|
|
|
class Manager implements ManagerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var SmartManagerRegistry |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $doctrine; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var PropertyAccessorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $propertyAccessor; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param SmartManagerRegistry $doctrine |
|
30
|
|
|
* @param PropertyAccessorInterface $propertyAccessor |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(SmartManagerRegistry $doctrine, PropertyAccessorInterface $propertyAccessor) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->doctrine = $doctrine; |
|
35
|
|
|
$this->propertyAccessor = $propertyAccessor; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function create($entityClass, array $defaultValues = array(), array $options = array()) |
|
42
|
|
|
{ |
|
43
|
|
|
$object = new $entityClass(); |
|
44
|
|
|
foreach ($defaultValues as $propertyPath => $value) { |
|
45
|
|
|
$this->propertyAccessor->setValue($object, $propertyPath, $value); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $object; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function find($entityClass, $id, array $options = array()) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->doctrine->getRepository($entityClass)->find($id); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function save($entity, array $options = array()) |
|
63
|
|
|
{ |
|
64
|
|
|
$em = $this->getManager($entity); |
|
65
|
|
|
$em->persist($entity); |
|
66
|
|
|
$em->flush(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function remove($entity) |
|
73
|
|
|
{ |
|
74
|
|
|
$em = $this->getManager($entity); |
|
75
|
|
|
$em->remove($entity); |
|
76
|
|
|
$em->flush(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the manager for an entity |
|
81
|
|
|
* |
|
82
|
|
|
* @param object|string $entity |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Doctrine\Common\Persistence\ObjectManager |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getManager($entity) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->doctrine->getManagerForClass( |
|
89
|
|
|
is_object($entity) ? ClassUtils::getClass($entity) : $entity |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|