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 * @implements JsonSerializable
51 function jsonSerialize() {
56 * @return \pq\Gateway\Table
70 * Check whether the row contains modifications
74 foreach ($this->cell
as $cell) {
75 if ($cell->isDirty()) {
83 $this->data
= $this->table
->find($this->criteria(), null, 1, 0)->current()->data
;
84 $this->cell
= array();
90 * @return \pq\Gateway\Row
92 protected function prime() {
93 $this->cell
= array();
94 foreach ($this->data
as $key => $val) {
95 $this->cell
[$key] = new Cell($this, $key, $val, true);
101 * Transform data array to where criteria
104 protected function criteria() {
106 foreach($this->data
as $k => $v) {
113 * Get an array of changed properties
116 protected function changes() {
118 foreach ($this->cell
as $name => $cell) {
119 if ($cell->isDirty()) {
120 $changes[$name] = $cell->get();
129 * @return \pq\Gateway\Cell
132 if (!isset($this->cell
[$p])) {
133 $this->cell
[$p] = new Cell($this, $p, isset($this->data
[$p]) ?
$this->data
[$p] : null);
135 return $this->cell
[$p];
143 function __set($p, $v) {
144 $this->__get($p)->set(($v instanceof Cell
) ?
$v->get() : $v);
148 * Create this row in the database
149 * @return \pq\Gateway\Row
152 $this->data
= $this->table
->create($this->changes())->current()->data
;
153 $this->cell
= array();
158 * Update this row in the database
159 * @return \pq\Gateway\Row
162 $this->data
= $this->table
->update($this->criteria(), $this->changes())->current()->data
;
163 $this->cell
= array();
168 * Delete this row in the database
169 * @return \pq\Gateway\Row
172 $this->data
= $this->table
->delete($this->criteria(), "*")->current()->data
;
173 return $this->prime();