X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FMapper%2FProperty%2FRef.php;h=8d9fce67bf6c0bec72f8f2b309f2034e75ba95aa;hb=861bb294d28aba35e011d9d35aad750d93c7dbd7;hp=84f39f41e7cbd660969983967158b5aa862e1f59;hpb=d4d84fb9b85f0216bd611946d59054a606c71960;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Mapper/Property/Ref.php b/lib/pq/Mapper/Property/Ref.php index 84f39f4..8d9fce6 100644 --- a/lib/pq/Mapper/Property/Ref.php +++ b/lib/pq/Mapper/Property/Ref.php @@ -4,6 +4,7 @@ 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; @@ -11,12 +12,22 @@ 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)) { @@ -27,9 +38,17 @@ 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); if (!$rel = $map->relOf($this->container, $this->refName)) { throw new UnexpectedValueException( sprintf("Unrelated reference from %s to %s with name %s", @@ -45,6 +64,11 @@ 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) {