1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf; |
10
|
|
|
|
11
|
|
|
use Pimf\DataMapper\Base; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Based on PDO it is a general manager for data persistence and object relational mapping. |
15
|
|
|
* |
16
|
|
|
* @package Pimf |
17
|
|
|
* @author Gjero Krsteski <[email protected]> |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class EntityManager extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string The namespace name of data-mappers repository. |
24
|
|
|
*/ |
25
|
|
|
protected $prefix; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \PDO $pdo |
29
|
|
|
* @param string $prefix The data-mappers repository name. |
30
|
|
|
*/ |
31
|
|
|
public function __construct(\PDO $pdo, $prefix = '\Pimf') |
32
|
|
|
{ |
33
|
|
|
parent::__construct($pdo); |
34
|
|
|
$this->prefix = $prefix . '\DataMapper\\'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $entity The name of the data-mapper class. |
39
|
|
|
* |
40
|
|
|
* @return Base |
41
|
|
|
* @throws \BadMethodCallException If no entity fount at the repository. |
42
|
|
|
*/ |
43
|
|
|
public function load($entity) |
44
|
|
|
{ |
45
|
|
|
$entity = $this->prefix . ucfirst($entity); |
46
|
|
|
|
47
|
|
|
if (true === $this->identityMap->hasId($entity)) { |
48
|
|
|
return $this->identityMap->getObject($entity); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!class_exists($entity)) { |
52
|
|
|
throw new \BadMethodCallException('entity "' . $entity . '" not found at the data-mapper repository'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$model = new $entity($this->pdo); |
56
|
|
|
|
57
|
|
|
$this->identityMap->set($entity, $model); |
58
|
|
|
|
59
|
|
|
return $this->identityMap->getObject($entity); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function beginTransaction() |
66
|
|
|
{ |
67
|
|
|
return $this->pdo->beginTransaction(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function commitTransaction() |
74
|
|
|
{ |
75
|
|
|
return $this->pdo->commit(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function rollbackTransaction() |
82
|
|
|
{ |
83
|
|
|
return $this->pdo->rollBack(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $entity |
88
|
|
|
* |
89
|
|
|
* @return Base |
90
|
|
|
*/ |
91
|
|
|
public function __get($entity) |
92
|
|
|
{ |
93
|
|
|
return $this->load($entity); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Database |
98
|
|
|
*/ |
99
|
|
|
public function getPDO() |
100
|
|
|
{ |
101
|
|
|
return $this->pdo; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|