public function retrieveReferenced(IRecord $record) { if ($record->getState() === IRecord::STATE_NEW) { return array(); } $cls = $this->referencedEntity; return $cls::findAll(array($this->column => $record->getPrimary())); }
public function saveReferenced(IRecord $record, $data) { $db = $this->mapper->getDb(); $ids = $db->delete($this->connectingTable)->where("%n = %i", $this->localKey, $record->getPrimary())->execute(); if (count($data)) { $q[] = "insert into [{$this->connectingTable}]"; foreach ($data as $referencedRecord) { $referencedRecord->save(); $q[] = array($this->localKey => $record->getPrimary(), $this->referencedKey => $referencedRecord->getPrimary()); } $db->query($q); } }
/** * Fix order before update * @param IRecord record */ public function fixOrderBeforeUpdate(IRecord $record) { if ($record->isValueModified($this->orderColumn) || isset($this->groupColumn) && $record->isValueModified($this->groupColumn)) { $original = $record->find($record->getPrimary()); $columns[] = $this->orderColumn; if (isset($this->groupColumn)) { $columns[] = $this->groupColumn; } $original->loadValues($columns); $db = $record->getMapper()->getDb(); $fluent = $db->update($record->getMapper()->getTable(), array($this->orderColumn . "%sql" => array("%n - 1", $this->orderColumn)))->where("%n > %i", $this->orderColumn, $original->{$this->orderColumn}); if (isset($this->groupColumn)) { $type = $record->getConfig()->getType($this->groupColumn); $fluent->where("%n = %{$type}", $this->groupColumn, $original->{$this->groupColumn}); } $fluent->execute(); $fluent = $db->update($record->getMapper()->getTable(), array($this->orderColumn . "%sql" => array("%n + 1", $this->orderColumn)))->where("%n >= %i", $this->orderColumn, $record->{$this->orderColumn}); if (isset($this->groupColumn)) { $fluent->where("%n = %" . $record->getConfig()->getType($this->groupColumn), $this->groupColumn, $record->{$this->groupColumn}); } $fluent->execute(); } }
/** * Hash specified column with specified hash function * @param IRecord $record */ public function hashColumn(IRecord $record) { if ($record->isValueModified($this->column)) { $record->{$this->column} = call_user_func($this->hashFunction, $record->{$this->column}); } }
/** * Remove cached html from cache if source is modified * @param IRecord record */ public function clearCacheIfModified(IRecord $record) { if ($record->isValueModified($this->source)) { $this->clearCache($record); } }
/** * Delete record * @param IRecord record */ public function delete(IRecord $record) { try { $record->onBeforeDelete($record); $this->getDb()->delete($this->table)->where($record->getValues($this->getConfig()->getPrimaryColumns()))->execute(); // set state $record->setState(IRecord::STATE_DELETED); $record->onAfterDelete($record); } catch (Exception $e) { throw new \ModelException("Delete failed. " . $e->getMessage(), $e->getCode(), $e); } }