5 use InvalidArgumentException
;
10 class Storage
implements StorageInterface
13 * The mapping of this storage
25 * The underlying table gateway
31 * Create a storage for $map
32 * @param Mapper $mapper
33 * @param string $class
35 function __construct(Mapper
$mapper, $class) {
36 $this->mapper
= $mapper;
37 $this->map
= $mapper->mapOf($class);
38 $this->gateway
= $this->map
->getGateway();
47 $id = $this->gateway
->getIdentity();
48 if (count($id) == 1 && is_scalar($pk)) {
50 } elseif (is_array($pk) && count($pk) === count($id)) {
53 throw InvalidArgumentException(
54 "Insufficient identity provided; not all fields of %s are provided in %s",
55 json_encode($id->getColumns()), json_encode($pk));
58 $keys = array_map(function($v) {
60 }, $id->getColumns());
62 $rowset = $this->gateway
->find(array_combine($keys, $vals));
64 return $this->map
->map($rowset->current());
70 * @param string $order
75 function find($where = [], $order = null, $limit = null, $offset = null) {
76 /* @var pq\Gateway\Rowset $rowset */
77 $rowset = $this->gateway
->find($where, $order, $limit, $offset);
78 return $this->map
->mapAll($rowset);
83 * @param object $object
84 * @param string $refName
87 function by($object, $refName) {
88 $row = $this->mapper
->mapOf($object)->getObjects()->getRow($object);
89 $this->map
->refOf($row, $refName, $objects);
90 return current($objects);
95 * @param object $object
96 * @param string $refName
99 function of($object, $refName) {
100 $row = $this->mapper
->mapOf($object)->getObjects()->getRow($object);
101 $this->map
->allOf($row, $refName, $objects);
107 * @param object $object
109 function delete($object) {
110 $cache = $this->map
->getObjects();
111 $row = $cache->asRow($object)->delete();
112 $cache->resetObject($row);
113 $cache->resetRow($object);
118 * @param object $object
120 function save($object) {
121 $this->map
->unmap($object);
125 * Buffer in a transaction
128 switch ($this->gateway
->getConnection()->transactionStatus
) {
129 case Connection
::TRANS_INTRANS
:
132 $this->gateway
->getQueryExecutor()->execute(new \pq\Query\
Writer("START TRANSACTION"));
140 switch ($this->gateway
->getConnection()->transactionStatus
) {
141 case Connection
::TRANS_IDLE
:
144 $this->gateway
->getQueryExecutor()->execute(new \pq\Query\
Writer("COMMIT"));
152 switch ($this->gateway
->getConnection()->transactionStatus
) {
153 case Connection
::TRANS_IDLE
:
156 $this->gateway
->getQueryExecutor()->execute(new \pq\Query\
Writer("ROLLBACK"));
158 $this->map
->getObjects()->reset();