X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FMapper%2FProperty%2FRef.php;h=8d9fce67bf6c0bec72f8f2b309f2034e75ba95aa;hb=5c080bb03b5fee867da94cb3c1108d59529f741a;hp=851df41e8aaefe3580d1ed0731d07c10f4760aa0;hpb=e6da4c7430e5c5b4ba482206e42aeebb4363d2b3;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Mapper/Property/Ref.php b/lib/pq/Mapper/Property/Ref.php index 851df41..8d9fce6 100644 --- a/lib/pq/Mapper/Property/Ref.php +++ b/lib/pq/Mapper/Property/Ref.php @@ -4,18 +4,30 @@ namespace pq\Mapper\Property; use pq\Gateway\Row; use pq\Mapper\Mapper; +use pq\Mapper\PropertyInterface; use pq\Mapper\RefProperty; use pq\Mapper\RefPropertyInterface; +use UnexpectedValueException; class Ref implements RefPropertyInterface { use RefProperty; - + + /** + * Create a parent row mapping + * @param Mapper $mapper + * @param string $property + */ function __construct(Mapper $mapper, $property) { $this->mapper = $mapper; $this->property = $property; } + /** + * Read the parent object + * @param Row $row + * @param object $objectToUpdate + */ function read(Row $row, $objectToUpdate) { $val = $this->extract($objectToUpdate); if (!isset($val)) { @@ -26,10 +38,24 @@ class Ref implements RefPropertyInterface } } + /** + * Write the parent row's foreign key + * @param object $object + * @param Row $rowToUpdate + * @throws UnexpectedValueException + */ function write($object, Row $rowToUpdate) { + if (!$ref = $this->extract($object)) { + return; + } $map = $this->mapper->mapOf($this->refClass); - $ref = $this->extract($object); - $rel = $map->relOf($this->container, $this->refName); + if (!$rel = $map->relOf($this->container, $this->refName)) { + throw new UnexpectedValueException( + sprintf("Unrelated reference from %s to %s with name %s", + $this->container->getGateway()->getName(), + $map->getGateway()->getName(), + $this->refName)); + } foreach ($rel as $fgn => $col) { foreach ($this->findFieldProperty($col) as $property) { $value = $property->extract($ref); @@ -38,38 +64,15 @@ class Ref implements RefPropertyInterface } } + /** + * Find the property exposing $col + * @param string $col + * @return PropertyInterface[] + */ private function findFieldProperty($col) { $map = $this->mapper->mapOf($this->refClass); return array_filter($map->getProperties(), function($property) use($col) { return $property->exposes($col); }); } - - - function read2(RowGateway $row) { - #echo __METHOD__." ".$this; - $map = $this->getRefMap(); - $rel = $this->container->getGateway()->getRelation( - $map->getGateway()->getName(), $this->refName); - $key = array_combine($rel->referencedColumns, array_map(function($c) use($row) { - return $row->$c->get(); - }, $rel->foreignColumns)); - if (($obj = $this->mapper->objectOfRowId($this->refClass, $key))) { - yield $this->property => $obj; - } else foreach ($map->getGateway()->by($row, $this->refName) as $row) { - yield $this->property => $this->mapper->objectOf($this->refClass, $row); - } - } - - function write2($object) { - #echo __METHOD__." ".$this; - $map = $this->getRefMap(); - $rel = $this->container->getGateway()->getRelation( - $map->getGateway()->getName(), $this->refName); - $ref = $this->extract($object); - foreach ($rel as $fgn => $col) { - $fld = $map->getFieldMapping($col); - yield $fgn => $fld->extract($ref); - } - } } \ No newline at end of file