public function save()
{
$this->events()->trigger(__CLASS__, 'before.save', $this);
$arraySave = array('name' => $this->getName(), 'updated_at' => new Expression('NOW()'), 'description' => $this->getDescription(), 'icon_id' => $this->getIconId(), 'default_view_id' => $this->getDefaultViewId(), 'user_id' => $this->getUserId());
try {
$id = $this->getId();
if (empty($id)) {
$arraySave['created_at'] = new Expression('NOW()');
$this->insert($arraySave);
$this->setId($this->getLastInsertId());
} else {
$this->update($arraySave, array('id' => (int) $this->getId()));
}
$delete = new Sql\Delete();
$delete->from('document_type_view');
$delete->where(array('document_type_id' => (int) $this->getId()));
$this->execute($delete);
foreach ($this->views as $viewId) {
if (empty($viewId)) {
continue;
}
$insert = new Sql\Insert();
$insert->into('document_type_view')->values(array('document_type_id' => $this->getId(), 'view_id' => $viewId));
$this->execute($insert);
}
$delete = new Sql\Delete();
$delete->from('document_type_dependency');
$delete->where->equalTo('parent_id', (int) $this->getId());
$this->execute($delete);
$dependencies = $this->getDependencies();
if (!empty($dependencies)) {
foreach ($dependencies as $childrenId) {
$insert = new Sql\Insert();
$insert->into('document_type_dependency')->values(array('parent_id' => $this->getId(), 'children_id' => $childrenId));
$this->execute($insert);
}
}
$this->events()->trigger(__CLASS__, 'after.save', $this);
return $this->getId();
} catch (\Exception $e) {
$this->events()->trigger(__CLASS__, 'after.save.failed', $this);
throw new \Gc\Exception($e->getMessage(), $e->getCode(), $e);
}
}
/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @return void */ public function setUp() { $this->init(); $this->view = ViewModel::fromArray(array('name' => 'View Name', 'identifier' => 'View identifier', 'description' => 'View Description', 'content' => 'View Content')); $this->view->save(); $this->layout = LayoutModel::fromArray(array('name' => 'Layout Name', 'identifier' => 'Layout identifier', 'description' => 'Layout Description', 'content' => 'Layout Content')); $this->layout->save(); $this->documentType = DocumentTypeModel::fromArray(array('name' => 'Document Type Name', 'description' => 'Document Type description', 'icon_id' => 1, 'defaultview_id' => $this->view->getId(), 'user_id' => $this->user->getId())); $this->documentType->save(); $this->document = DocumentModel::fromArray(array('name' => 'Document name', 'url_key' => 'url-key', 'status' => DocumentModel::STATUS_ENABLE, 'show_in_nav' => true, 'user_id' => $this->user->getId(), 'document_type_id' => $this->documentType->getId(), 'view_id' => $this->view->getId(), 'layout_id' => $this->layout->getId(), 'parent_id' => null)); $this->document->save(); ModuleModel::install(Registry::get('Application')->getServiceManager()->get('CustomModules'), 'Blog'); }