X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpq%2FMapper%2FProperty%2FRef.php;fp=lib%2Fpq%2FMapper%2FProperty%2FRef.php;h=851df41e8aaefe3580d1ed0731d07c10f4760aa0;hb=e6da4c7430e5c5b4ba482206e42aeebb4363d2b3;hp=0000000000000000000000000000000000000000;hpb=0861b6cd0d8f86add1a068fd3d9740428c8418cd;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Mapper/Property/Ref.php b/lib/pq/Mapper/Property/Ref.php new file mode 100644 index 0000000..851df41 --- /dev/null +++ b/lib/pq/Mapper/Property/Ref.php @@ -0,0 +1,75 @@ +mapper = $mapper; + $this->property = $property; + } + + function read(Row $row, $objectToUpdate) { + $val = $this->extract($objectToUpdate); + if (!isset($val)) { + $map = $this->mapper->mapOf($this->refClass); + $ref = $map->refOf($row, $this->refName, $objects)->current(); + $this->assign($objectToUpdate, current($objects)); + $map->map($ref); + } + } + + function write($object, Row $rowToUpdate) { + $map = $this->mapper->mapOf($this->refClass); + $ref = $this->extract($object); + $rel = $map->relOf($this->container, $this->refName); + foreach ($rel as $fgn => $col) { + foreach ($this->findFieldProperty($col) as $property) { + $value = $property->extract($ref); + $rowToUpdate->$fgn = $value; + } + } + } + + 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