5 class Row
implements \JsonSerializable
8 * @var \pq\Gateway\Table
20 protected $cell = array();
23 * @param \pq\Gateway\Table $table
25 * @param bool $prime whether to mark all columns as modified
27 function __construct(Table
$table, array $data = null, $prime = false) {
28 $this->table
= $table;
29 $this->data
= (array) $data;
39 * @return \pq\Gateway\Row
41 function __invoke(array $data) {
44 return $that->prime();
48 * Export current state as an array
50 * @throws \UnexpectedValueException if a cell has been modified by an expression
53 $export = array_merge($this->data
, $this->cell
);
54 foreach ($export as &$val) {
55 if ($val instanceof Cell
) {
57 throw new \
UnexpectedValueException("Cannot export an SQL expression");
66 * Export current state with security sensitive data removed. You should override that, just
67 * calls export() by default.
70 function exportPublic() {
71 return $this->export();
75 * @implements JsonSerializable
78 function jsonSerialize() {
79 return $this->exportPublic();
83 * @return \pq\Gateway\Table
97 * Check whether the row contains modifications
101 foreach ($this->cell
as $cell) {
102 if ($cell->isDirty()) {
110 $this->data
= $this->table
->find($this->criteria(), null, 1, 0)->current()->data
;
111 $this->cell
= array();
116 * Fill modified cells
117 * @return \pq\Gateway\Row
119 protected function prime() {
120 $this->cell
= array();
121 foreach ($this->data
as $key => $val) {
122 $this->cell
[$key] = new Cell($this, $key, $val, true);
128 * Transform data array to where criteria
131 protected function criteria() {
133 foreach($this->data
as $k => $v) {
140 * Get an array of changed properties
143 protected function changes() {
145 foreach ($this->cell
as $name => $cell) {
146 if ($cell->isDirty()) {
147 $changes[$name] = $cell->get();
154 * Get a cell or parent rows
156 * @return \pq\Gateway\Cell|\pq\Gateway\Rowset
159 if ($this->table
->hasRelation($p)) {
160 return $this->table
->by($this, $p);
162 if (!isset($this->cell
[$p])) {
163 $this->cell
[$p] = new Cell($this, $p, isset($this->data
[$p]) ?
$this->data
[$p] : null);
165 return $this->cell
[$p];
173 function __set($p, $v) {
174 $this->__get($p)->set($v);
181 function __unset($p) {
182 unset($this->data
[$p]);
183 unset($this->cell
[$p]);
187 * Check if a cell isset
191 function __isset($p) {
192 return isset($this->data
[$p]) ||
isset($this->cell
[$p]);
196 * Get child rows of this row by foreign key
197 * @see \pq\Gateway\Table::of()
198 * @param string $foreign
199 * @param array $args [order, limit, offset]
200 * @return \pq\Gateway\Rowset
202 function __call($foreign, array $args) {
203 array_unshift($args, $this);
204 $table = forward_static_call(array(get_class($this->getTable()), "resolve"), $foreign);
205 return call_user_func_array(array($table, "of"), $args);
209 * Create this row in the database
210 * @return \pq\Gateway\Row
213 $this->data
= $this->table
->create($this->changes())->current()->data
;
214 $this->cell
= array();
219 * Update this row in the database
220 * @return \pq\Gateway\Row
223 $this->data
= $this->table
->update($this->criteria(), $this->changes())->current()->data
;
224 $this->cell
= array();
229 * Delete this row in the database
230 * @return \pq\Gateway\Row
233 $this->data
= $this->table
->delete($this->criteria(), "*")->current()->data
;
234 return $this->prime();