* The mapping of this storage
* @var MapInterface
*/
- var $map;
+ private $map;
/**
- * The underlying table gateway
- * @var Table
+ * The mapper
+ * @var Mapper
*/
- private $gateway;
+ private $mapper;
/**
- * Buffered transaction
- * @var Transaction
+ * The underlying table gateway
+ * @var Table
*/
- private $xaction;
+ private $gateway;
/**
* Create a storage for $map
- * @param MapInterface $map
+ * @param Mapper $mapper
+ * @param string $class
*/
- function __construct(MapInterface $map) {
- $this->map = $map;
- $this->gateway = $map->getGateway();
+ function __construct(Mapper $mapper, $class) {
+ $this->mapper = $mapper;
+ $this->map = $mapper->mapOf($class);
+ $this->gateway = $this->map->getGateway();
}
/**
function get($pk) {
$id = $this->gateway->getIdentity();
if (count($id) == 1 && is_scalar($pk)) {
- $pk = [current($id->getColumns()) => $pk];
- } elseif (!is_array($pk) || count($pk) !== count($id)) {
+ $vals = [$pk];
+ } elseif (is_array($pk) && count($pk) === count($id)) {
+ $vals = $pk;
+ } else {
throw InvalidArgumentException(
"Insufficient identity provided; not all fields of %s are provided in %s",
json_encode($id->getColumns()), json_encode($pk));
}
- $where = [];
- foreach ($pk as $k => $v) {
- $where["$k="] = $v;
- }
- $rowset = $this->gateway->find($where);
+ $keys = array_map(function($v) {
+ return "$v=";
+ }, $id->getColumns());
+
+ $rowset = $this->gateway->find(array_combine($keys, $vals));
return $this->map->map($rowset->current());
}
return $this->map->mapAll($rowset);
}
+ /**
+ * Find parent
+ * @param object $object
+ * @param string $refName
+ * @return object
+ */
+ function by($object, $refName) {
+ $row = $this->mapper->mapOf($object)->getObjects()->getRow($object);
+ $this->map->refOf($row, $refName, $objects);
+ return current($objects);
+ }
+
+ /**
+ * Find childs
+ * @param object $object
+ * @param string $refName
+ * @return array
+ */
+ function of($object, $refName) {
+ $row = $this->mapper->mapOf($object)->getObjects()->getRow($object);
+ $this->map->allOf($row, $refName, $objects);
+ return $objects;
+ }
+
/**
* Delete
* @param object $object
*/
function find($where = [], $order = null, $limit = null, $offset = null);
+ /**
+ * Find parent
+ * @param object $object
+ * @param string $refName
+ * @return object
+ */
+ function by($object, $refName);
+
+ /**
+ * Find child rows
+ * @param object $object
+ * @param string $refName
+ * @return array
+ */
+ function of($object, $refName);
+
/**
* Delete
* @param object $object
$this->storage->discard();
$this->assertEquals("the day before", $this->storage->get(1)->data);
}
+
+ function testOf() {
+ $obj = $this->storage->get(1);
+ $ref = new Storage($this->mapper, RefTestModel::class);
+ $this->assertSame($obj->ref1, $ref->of($obj, "test"));
+ $this->assertSame($obj->ref2, $ref->of($obj, "another_test"));
+ }
+
+ function testBy() {
+ $ref = new Storage($this->mapper, RefTestModel::class);
+ $obj = $ref->get([2,2]);
+ $this->assertSame($obj->one, $this->storage->by($obj, "test"));
+ $this->assertSame($obj->two, $this->storage->by($obj, "another_test"));
+ }
}