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); } }
public function retrieveReferenced(IRecord $record) { if ($record->getState() === IRecord::STATE_NEW) { return array(); } $cls = $this->referencedEntity; return $cls::findAll(array($this->column => $record->getPrimary())); }
/** * 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(); } }