5 class Row
implements \JsonSerializable
8 * @var \pq\Gateway\Table
20 protected $mods = 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;
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
71 * @return \pq\Gateway\Row
73 protected function prime() {
74 $this->mods
= array();
75 foreach ($this->data
as $key => $val) {
76 $this->mods
[$key] = new Cell($this, $key, $val);
82 * Transform data array to where criteria
86 protected function criteria() {
88 array_walk($this->data
, function($v, $k) use (&$where) {
94 protected function changes() {
96 foreach ($this->mods
as $name => $cell) {
97 $changes[$name] = $cell->get();
105 * @return \pq\Gateway\Cell
108 if (!isset($this->mods
[$p])) {
109 $this->mods
[$p] = new Cell($this, $p, $this->data
[$p]);
111 return $this->mods
[$p];
119 function __set($p, $v) {
120 $this->__get($p)->set(($v instanceof Cell
) ?
$v->get() : $v);
124 * Create this row in the database
125 * @return \pq\Gateway\Row
128 $this->data
= $this->table
->create($this->changes())->current()->data
;
129 $this->mods
= array();
134 * Update this row in the database
135 * @return \pq\Gateway\Row
138 $this->data
= $this->table
->update($this->criteria(), $this->changes())->current()->data
;
139 $this->mods
= array();
144 * Delete this row in the database
145 * @return \pq\Gateway\Row
148 $this->data
= $this->table
->delete($this->criteria(), "*")->current()->data
;
149 return $this->prime();