/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @return void */ protected function setUp() { $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->user = UserModel::fromArray(array('lastname' => 'User test', 'firstname' => 'User test', 'email' => '*****@*****.**', 'login' => 'test', 'user_acl_role_id' => 1)); $this->user->setPassword('test'); $this->user->save(); $this->object = Model::fromArray(array('name' => 'Document Type Name', 'description' => 'Document Type description', 'icon_id' => 1, 'defaultview_id' => $this->view->getId(), 'user_id' => $this->user->getId())); $this->object->save(); $this->documentTypeChildren = Model::fromArray(array('name' => 'Document Type children Name', 'description' => 'Document Type children description', 'icon_id' => 1, 'defaultview_id' => $this->view->getId(), 'user_id' => $this->user->getId())); $this->documentTypeChildren->save(); $insert = new Insert(); $insert->into('document_type_dependency')->values(array('parent_id' => $this->object->getId(), 'children_id' => $this->documentTypeChildren->getId())); $this->object->execute($insert); $insert = new Insert(); $insert->into('document_type_view')->values(array('view_id' => $this->view->getId(), 'document_type_id' => $this->object->getId())); $this->object->execute($insert); }
/** * @covers Zend\Db\Sql\Insert::getSqlString * @todo Implement testGetSqlString(). */ public function testGetSqlString() { $this->insert->into('foo')->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'))); $this->assertEquals('INSERT INTO "foo" ("bar", "boo") VALUES (\'baz\', NOW())', $this->insert->getSqlString()); }
/** * @todo add $columns support * * @param Insert $insert * @return int * @throws Exception\RuntimeException */ protected function executeInsert(Insert $insert) { $insertState = $insert->getRawState(); if ($insertState['table'] != $this->table) { throw new Exception\RuntimeException('The table name of the provided Insert object must match that of the table'); } // apply preInsert features $this->featureSet->apply(EventFeatureEventsInterface::EVENT_PRE_INSERT, [$insert]); // Most RDBMS solutions do not allow using table aliases in INSERTs // See https://github.com/zendframework/zf2/issues/7311 $unaliasedTable = false; if (is_array($insertState['table'])) { $tableData = array_values($insertState['table']); $unaliasedTable = array_shift($tableData); $insert->into($unaliasedTable); } $statement = $this->sql->prepareStatementForSqlObject($insert); $result = $statement->execute(); $this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue(); // apply postInsert features $this->featureSet->apply(EventFeatureEventsInterface::EVENT_POST_INSERT, [$statement, $result]); // Reset original table information in Insert instance, if necessary if ($unaliasedTable) { $insert->into($insertState['table']); } return $result->getAffectedRows(); }
/** * Build a sql object to insert a contact request record * * @param ContactEntity $contact * @return Insert */ protected function contactInsert(ContactEntity $contact) { $insert = new Insert(); $insert->into('contact_request')->values(['name' => $contact->getName(), 'email' => $contact->getEmail(), 'message' => $contact->getMessage()]); return $insert; }
/** * @group ZF2-536 */ public function testValuesMerge() { $this->insert->into('foo')->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null)); $this->insert->into('foo')->values(array('qux' => 100), Insert::VALUES_MERGE); $this->assertEquals('INSERT INTO "foo" ("bar", "boo", "bam", "qux") VALUES (\'baz\', NOW(), NULL, \'100\')', $this->insert->getSqlString(new TrustingSql92Platform())); }
/** * Get a sql object to insert a new blog post * * @param PostEntity $post * @return Insert */ protected function getPostInsert(PostEntity $post) { $insert = new Insert(); $insert->into(new TableIdentifier('blog_post'))->values(['title' => $post->getTitle(), 'author' => $post->getAuthor(), 'content' => $post->getContent(), 'is_visible' => $post->getIsVisible()]); return $insert; }
/** * Build a sql object to insert a new blog post asset * * @param AssetEntity $asset * @return Insert */ protected function assetInsert(AssetEntity $asset) { $insert = new Insert(); $insert->into(new TableIdentifier('blog_asset'))->values(['blog_post_id' => $asset->getBlogPostId(), 'directory' => $asset->getDirectory(), 'baseurl' => $asset->getBaseurl(), 'filename' => $asset->getFilename()]); return $insert; }
/** * Install module * * @param ModuleManager $moduleManager Module manager * @param string $moduleName Module Name * * @return boolean|integer */ public static function install(ModuleManager $moduleManager, $moduleName) { try { $object = $moduleManager->loadModule($moduleName); } catch (\Exception $e) { //Don't care } if (empty($object) or !$object->install()) { return false; } $model = new Model(); $model->setName($moduleName); $model->save(); $select = new Sql\Select(); $select->from('user_acl_resource')->columns(array('id'))->where->equalTo('resource', 'modules'); $insert = new Sql\Insert(); $insert->into('user_acl_permission')->values(array('permission' => $moduleName, 'user_acl_resource_id' => $model->fetchOne($select))); $model->execute($insert); return $model->getId(); }
/** * Save properties * * @return boolean */ public function save() { $this->events()->trigger(__CLASS__, 'before.save', $this); if (!empty($this->data['document_type_id'])) { $this->delete(); $insert = new Insert(); $insert->into('document_type_view'); foreach ($this->getElements() as $view) { $insert->values(array('document_type_id' => $this->getDocumentTypeId(), 'view_id' => $view->getId())); $this->execute($insert); } $this->events()->trigger(__CLASS__, 'after.save', $this); return true; } $this->events()->trigger(__CLASS__, 'after.save.failed', $this); return false; }
/** * Save document type model * * @return integer */ 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); } }
/** * Set config value * * @param string $source Source * @param array $destinations Destinations * * @return boolean */ public function setValue($source, array $destinations) { if (is_numeric($source)) { $row = $this->fetchRow($this->select(array('id' => $source))); if (empty($row)) { return false; } $sourceId = $row['id']; } else { $row = $this->fetchRow($this->select(array('source' => $source))); if (!empty($row)) { $sourceId = $row['id']; } else { $this->insert(array('source' => $source)); $sourceId = $this->getLastInsertId(); } } foreach ($destinations as $destination) { if (empty($destination['locale']) or empty($destination['value'])) { continue; } $select = new Select(); $select->from('core_translate_locale'); $select->where->equalTo('locale', $destination['locale']); $select->where->equalTo('core_translate_id', $sourceId); $row = $this->fetchRow($select); if (!empty($row)) { $destination['dst_id'] = $row['id']; } if (!empty($destination['dst_id'])) { $update = new Update('core_translate_locale'); $update->set(array('destination' => $destination['value'], 'locale' => $destination['locale'])); $update->where->equalTo('id', $destination['dst_id']); $this->execute($update); } else { $insert = new Insert(); $insert->into('core_translate_locale')->values(array('destination' => $destination['value'], 'locale' => $destination['locale'], 'core_translate_id' => $sourceId)); $this->execute($insert); } } return true; }
/** * Get url id * * @param string $requestUri Request URI * @param string $referer Referer * * @return integer */ public function getUrlId($requestUri, $referer) { $select = new Select(); $select->from('log_url_info')->where->equalTo('url', $requestUri); if (is_null($referer)) { $select->where->isNull('referer'); } else { $select->where->equalTo('referer', $referer); } $urlInfo = $this->fetchRow($select); if (!empty($urlInfo['id'])) { $urlId = $urlInfo['id']; } else { $insert = new Insert(); $insert->into('log_url_info')->values(array('url' => $requestUri, 'referer' => $referer)); $this->execute($insert); $urlId = $this->getLastInsertId('log_url_info'); } return $urlId; }