Skip to content

Commit 9592992

Browse files
committed
MockEntityList does now accept values to add
1 parent da3ab4e commit 9592992

File tree

10 files changed

+91
-73
lines changed

10 files changed

+91
-73
lines changed

PPA.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ public function getConnection() {
107107
}
108108

109109
/**
110-
* This function is provided by PPA to show variables in a more human readable
111-
* way.
110+
* This function is provided by PPA to show variables in a more human readable way.
112111
*
113112
* @param mixed $param
114113
*/

README.md

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ The PHP Persistence API (`PPA`) is an Interface for PHP-Applications to access O
77

88
***
99

10-
## [v1.0.0 released!](https://github.com/sweiguny/PHP-Persistence-API/releases/tag/v1.0.0)
10+
### [v1.0.0 released!](https://github.com/sweiguny/PHP-Persistence-API/releases/tag/v1.0.0)
1111

1212
***
1313

14-
**Features:**
15-
- Easy to embed in your project
14+
#### Features:
15+
- [Easy to embed in your project](https://github.com/sweiguny/PHP-Persistence-API/wiki/Embedding-PPA)
1616
- Configure entities via [annotations](https://github.com/sweiguny/PHP-Persistence-API/wiki/Annotations-&-Parameters) (no xml!)
1717
- Relations
1818
- OneToOne
1919
- OneToMany
2020
- ManyToMany
2121
- [TypedQueries](https://github.com/sweiguny/PHP-Persistence-API/wiki/TypedQuery) and PreparedQueries.
22-
- Transactions
2322
- CRUD
23+
- Transactions
2424
- Good performance
2525
- Eager- & Lazy-loading
2626
- A neat [WIKI](https://github.com/sweiguny/PHP-Persistence-API/wiki)
@@ -30,52 +30,41 @@ The PHP Persistence API (`PPA`) is an Interface for PHP-Applications to access O
3030

3131
***
3232

33-
## Examples:
33+
### Examples:
3434

3535
**Configuring your entities:**
3636

37+
namespace PPA\examples\entity;
3738
use PPA\core\Entity;
3839

3940
/**
40-
* @table(name="user")
41+
* @table(name="role")
4142
*/
42-
class User extends Entity {
43+
class Role extends Entity {
4344

4445
/**
4546
* @id
4647
* @column(name="id")
4748
*/
4849
private $id;
4950

50-
/**
51-
* @Column(name="username")
52-
*/
53-
private $username;
54-
55-
/**
56-
* @Column(name="password")
57-
*/
58-
private $password;
51+
/** @column(name="name") */
52+
private $name;
5953

6054
/**
61-
* @Column(name="role_id");
62-
* @oneToOne(fetch="lazy", mappedBy = "_PPA_examples_entity_Role")
55+
* @manyToMany(fetch = "eager", mappedBy = "_PPA_examples_entity_Right")
56+
* @joinTable(name = "role2right", column = "role_id", x_column = "right_id")
6357
*/
64-
private $role;
65-
66-
public function getRole() {
67-
return $this->role;
68-
}
58+
private $rights = array();
6959
}
7060

7161
***
7262

7363
**Retrieving data:**
7464

75-
use PPA\core\query\TypedQuery;
76-
77-
// A TypedQuery can automatically resolve all the relations and give an appropriate output.
78-
$query = new TypedQuery("SELECT * FROM `role` WHERE id = 2", "\\PPA\\examples\\entity\\Role");
65+
A TypedQuery can automatically resolve all the relations and give an appropriate output.
66+
67+
$query = new \PPA\core\query\TypedQuery("SELECT * FROM `role` WHERE id = 2", "\\PPA\\examples\\entity\\Role");
7968
$query->getSingleResult();
8069

8170
Returns:
@@ -95,24 +84,5 @@ The PHP Persistence API (`PPA`) is an Interface for PHP-Applications to access O
9584
[id:PPA\examples\entity\Right:private] => 1
9685
[desc:PPA\examples\entity\Right:private] => login
9786
)
98-
[2] => PPA\examples\entity\Right Object
99-
(
100-
[id:PPA\examples\entity\Right:private] => 2
101-
[desc:PPA\examples\entity\Right:private] => logout
102-
)
103-
[3] => PPA\examples\entity\Right Object
104-
(
105-
[id:PPA\examples\entity\Right:private] => 5
106-
[desc:PPA\examples\entity\Right:private] => create_order
107-
)
108-
[4] => PPA\examples\entity\Right Object
109-
(
110-
[id:PPA\examples\entity\Right:private] => 4
111-
[desc:PPA\examples\entity\Right:private] => delete_order
112-
)
11387
)
11488
)
115-
116-
***
117-
118-
For more examples, please see the [WIKI](https://github.com/sweiguny/PHP-Persistence-API/wiki).

core/EntityAnalyzer.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,39 @@ class EntityAnalyzer {
3131
*/
3232
private $analyzed = false;
3333

34+
/**
35+
* The property that represents the primary key in the table.
36+
*
37+
* @var EntityProperty
38+
*/
3439
private $primaryProperty;
40+
41+
/**
42+
* The name of the table to which the entity is mapped.
43+
*
44+
* @var string
45+
*/
3546
private $tableName;
47+
48+
/**
49+
* An array filled with properties indexed by the property name.
50+
*
51+
* @var array
52+
*/
3653
private $propertiesByName;
54+
55+
/**
56+
* An array filled with properties indexed by the column name.
57+
*
58+
* @var array
59+
*/
3760
private $propertiesByColumn;
61+
62+
/**
63+
* An array filled with all relations of the entity.
64+
*
65+
* @var array
66+
*/
3867
private $relations;
3968

4069
/**
@@ -80,8 +109,12 @@ public function doAnalysis() {
80109
if ($this->analyzed) {
81110
# TODO: trigger error and/or log message
82111
} else {
83-
84-
$annotations = $this->extractAnnotations($this->reflector->getDocComment());
112+
$primaryProperty = null;
113+
$propertiesByName = array();
114+
$propertiesByColumn = array();
115+
$relations = array();
116+
$properties = $this->reflector->getProperties();
117+
$annotations = $this->extractAnnotations($this->reflector->getDocComment());
85118

86119
if (isset($annotations["@table"]) && isset($annotations["@table"]["name"])) {
87120
$this->tableName = $annotations["@table"]["name"];
@@ -90,15 +123,6 @@ public function doAnalysis() {
90123
}
91124

92125

93-
# --------------------
94-
95-
96-
$primaryProperty = null;
97-
$propertiesByName = array();
98-
$propertiesByColumn = array();
99-
$relations = array();
100-
$properties = $this->reflector->getProperties();
101-
102126
foreach ($properties as $property) {
103127
$annotations = $this->extractAnnotations($property->getDocComment());
104128

@@ -145,6 +169,7 @@ public function doAnalysis() {
145169
if ($primaryProperty == null) {
146170
throw new AnnotationException("Entity '{$this->classname}' does not have an @id annotation.");
147171
}
172+
148173
$this->primaryProperty = $primaryProperty;
149174
$this->propertiesByName = $propertiesByName;
150175
$this->propertiesByColumn = $propertiesByColumn;
@@ -154,8 +179,10 @@ public function doAnalysis() {
154179
}
155180

156181
/**
157-
* @param array $annotations
158-
* @throws AnnotationException when some annotations are not compliant.
182+
* Gives an appropriate output of which combination is not proper.
183+
*
184+
* @param array $annotations The annotations of the properties.
185+
* @throws AnnotationException
159186
*/
160187
private function handleUncombinables(array $annotations) {
161188
if (isset($annotations["@joinTable"])) {

core/EntityFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ class EntityFactory {
99

1010
/**
1111
* Instantiates an instance of the given classname, but without calling the
12-
* constructor. It is necessary, that the class is a subclass of \PPA\Entity.
12+
* constructor. It is necessary, that the class is a subclass of \PPA\core\Entity.
1313
*
1414
* @param string $classname
1515
* @return Entity An instance of the classname.
16-
* @throws NoEntityException If the classname is not a subclass of \PPA\Entity.
16+
* @throws NoEntityException
1717
*/
1818
public static function create($classname) {
1919
$reflection = new ReflectionClass($classname);

core/EntityManager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static function getInstance() {
3636
*/
3737
private $emdm;
3838

39+
private function __clone() { }
3940
private function __construct() {
4041
$this->conn = PPA::getInstance()->getConnection();
4142
$this->emdm = EntityMetaDataMap::getInstance();
@@ -159,6 +160,12 @@ public function persist(Entity $entity) {
159160
}
160161
}
161162

163+
/**
164+
* Removes an entity from the database.
165+
*
166+
* @param Entity $entity The entity to remove from database.
167+
* @return int The number of affected rows.
168+
*/
162169
public function remove(Entity $entity) {
163170
$classname = get_class($entity);
164171
$tablename = $this->emdm->getTableName($classname);
@@ -170,6 +177,8 @@ public function remove(Entity $entity) {
170177
return $q->getSingleResult(array($primaryProperty->getValue($entity)));
171178
}
172179

180+
# TODO: outsource the following methods.
181+
173182
public function begin() {
174183
if ($this->inTransaction()) {
175184
throw new TransactionException("Already in an transaction.");

core/EntityProperty.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ public function setColumn($column) {
2323
$this->column = $column;
2424
}
2525

26+
/**
27+
* @param Relation $relation
28+
*/
2629
public function setRelation(Relation $relation) {
2730
$this->relation = $relation;
2831
}
2932

3033
/**
31-
*
3234
* @return Relation
3335
*/
3436
public function getRelation() {
3537
return $this->relation;
3638
}
3739

3840
/**
41+
* Checks if the property represents a relation to another entity.
3942
*
4043
* @return bool
4144
*/
@@ -44,17 +47,22 @@ public function hasRelation() {
4447
}
4548

4649
/**
50+
* Checks if the property is the primary.
4751
*
4852
* @return bool
4953
*/
5054
public function isPrimary() {
5155
return $this->isPrimary;
5256
}
5357

58+
/**
59+
* Defines the property as primary.
60+
*
61+
* @param bool $primary
62+
*/
5463
public function makePrimary($primary = true) {
5564
$this->isPrimary = (bool)$primary;
5665
}
57-
5866

5967
}
6068

core/mock/MockEntity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function __call($name, $arguments) {
5353
}
5454
}
5555

56+
/**
57+
* @return Entity The true entity instead of the mock.
58+
*/
5659
protected function exchange() {
5760
$entity = $this->query->getSingleResult($this->values);
5861
$this->property->setValue($this->owner, $entity);

core/mock/MockEntityList.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function __call($name, $arguments) {
4747
}
4848
}
4949

50+
/**
51+
* Exchanges the mock list with an array that contains true entities.
52+
*/
5053
protected function exchange() {
5154
if ($this->entities == null) {
5255
$this->entities = $this->query->getResultList($this->values);
@@ -65,11 +68,13 @@ public function offsetGet($offset) {
6568
}
6669

6770
public function offsetSet($offset, $value) {
68-
throw new BadMethodCallException("Not possible.");
71+
$this->exchange();
72+
return $this->entities[$offset] = $value;
6973
}
7074

7175
public function offsetUnset($offset) {
72-
throw new BadMethodCallException("Not possible.");
76+
$this->exchange();
77+
unset($this->entities[$offset]);
7378
}
7479

7580
public function count() {

examples/em.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313

1414
//$em->persist(new PPA\examples\entity\User());
1515

16-
//$query = new PPA\core\query\TypedQuery("select * from `role` where id=1", "\\PPA\\examples\\entity\\Role");
17-
//$role = $query->getSingleResult();
18-
19-
20-
$role = new PPA\examples\entity\Role("neu");
16+
$query = new PPA\core\query\TypedQuery("select * from `role` where id=1", "\\PPA\\examples\\entity\\Role");
17+
$role = $query->getSingleResult();
2118

2219
$role->addRight(new \PPA\examples\entity\Right("test"));
2320
$role->addRight(new \PPA\examples\entity\Right("test"));

examples/entity/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Role extends Entity {
2121
private $name;
2222

2323
/**
24-
* @manyToMany(fetch = "eager", mappedBy = "_PPA_examples_entity_Right")
24+
* @manyToMany(fetch = "lazy", mappedBy = "_PPA_examples_entity_Right")
2525
* @joinTable(name = "role2right", column = "role_id", x_column = "right_id")
2626
*/
2727
private $rights = array();

0 commit comments

Comments
 (0)