10 class Map
implements MapInterface
17 function __construct($class, Table
$gateway, PropertyInterface
...$properties) {
18 $this->class = $class;
19 $this->gateway
= $gateway;
20 $this->properties
= $properties;
21 foreach ($properties as $property) {
22 $property->setContainer($this);
24 $this->objects
= new ObjectManager($this);
31 function getObjects() {
32 return $this->objects
;
38 function getGateway() {
39 return $this->gateway
;
42 function getProperties() {
43 return $this->properties
;
46 function addProperty(PropertyInterface
$property) {
47 $property->setContainer($this);
48 $this->properties
[] = $property;
52 function allOf(Row
$row, $refName, &$objects = null) {
53 /* apply objectOf to populate the object cache */
54 return $this->gateway
->of($row, $refName)->apply(function($row) use(&$objects) {
55 $objects[] = $this->objects
->asObject($row);
59 function refOf(Row
$row, $refName, &$objects = null) {
61 $rel = $row->getTable()->getRelation($this->gateway
->getName(), $refName);
62 // FIXME: check if foreign key points to primary key
63 foreach ($rel as $fgn => $col) {
64 $rid[$col] = $row->$fgn->get();
66 $rid = $this->objects
->serializeRowId($rid);
67 if ($this->objects
->hasObject($rid)) {
68 $object = $this->objects
->getObjectById($rid);
69 $row = $this->objects
->getRow($object);
71 $rowset = new Rowset($this->gateway
);
72 return $rowset->append($row);
74 /* apply objectOf to populate the object cache */
75 return $this->gateway
->by($row, $refName)->apply(function($row) use(&$objects) {
76 $objects[] = $this->objects
->asObject($row);
80 function relOf(MapInterface
$map, $refName) {
81 return $map->getGateway()->getRelation(
82 $this->gateway
->getName(), $refName);
85 private function drain(array $deferred, callable
$exec) {
87 $cb = array_shift($deferred);
88 if (($cb = $exec($cb))) {
94 function map(Row
$row) {
96 $object = $this->objects
->asObject($row);
97 foreach ($this->properties
as $property) {
98 if (($cb = $property->read($row, $object))) {
102 $this->drain($deferred, function(callable
$cb) use($row, $object) {
103 return $cb($row, $object);
108 function mapAll(Rowset
$rows) {
110 foreach ($rows as $row) {
111 $objects[] = $this->map($row);
116 function unmap($object) {
119 $row = $this->objects
->asRow($object);
120 $upd = $this->objects
->rowId($row, true);
121 foreach ($this->properties
as $property) {
122 if (($cb = $property->write($object, $row))) {
126 foreach ($this->gateway
->getIdentity() as $col) {
127 if (null === $row->$col->get()
128 ||
($row->$col->isExpr() && $row->$col->get()->isNull()))
130 $row->$col = new Expr("DEFAULT");
133 if ($row->isDirty()) {
140 foreach ($this->properties
as $property) {
141 if (($cb = $property->read($row, $object))) {
145 $this->drain($deferred, function($cb) use($object, $row) {
146 return $cb($object, $row);
148 if ($row->isDirty()) {