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;
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()) {
84 * @return \pq\Gateway\Row
86 protected function prime() {
87 $this->cell
= array();
88 foreach ($this->data
as $key => $val) {
89 $this->cell
[$key] = new Cell($this, $key, $val, true);
95 * Transform data array to where criteria
98 protected function criteria() {
100 foreach($this->data
as $k => $v) {
107 * Get an array of changed properties
110 protected function changes() {
112 foreach ($this->cell
as $name => $cell) {
113 if ($cell->isDirty()) {
114 $changes[$name] = $cell->get();
123 * @return \pq\Gateway\Cell
126 if (!isset($this->cell
[$p])) {
127 $this->cell
[$p] = new Cell($this, $p, $this->data
[$p]);
129 return $this->cell
[$p];
137 function __set($p, $v) {
138 $this->__get($p)->set(($v instanceof Cell
) ?
$v->get() : $v);
142 * Create this row in the database
143 * @return \pq\Gateway\Row
146 $this->data
= $this->table
->create($this->changes())->current()->data
;
147 $this->cell
= array();
152 * Update this row in the database
153 * @return \pq\Gateway\Row
156 $this->data
= $this->table
->update($this->criteria(), $this->changes())->current()->data
;
157 $this->cell
= array();
162 * Delete this row in the database
163 * @return \pq\Gateway\Row
166 $this->data
= $this->table
->delete($this->criteria(), "*")->current()->data
;
167 return $this->prime();