/** * @param array|\DibiRow $article * @return bool */ public function save(&$article) { if (!isset($article['id'])) { $this->database->insert(self::TABLE, $article)->execute(); $article['id'] = $this->database->getInsertId(); } else { $this->database->update(self::TABLE, $article)->where(self::TABLE, '.id = %i', $article['id'])->execute(); } return $this->database->getAffectedRows() == 1; }
public function testFindByPrimary() { // find by primary // insert new article $this->db->insert("pages", array("name" => "Find test", "description" => "Find by primary", "text" => "Nějaký text.", "allowed" => true))->execute(); $id = $this->db->getInsertId(); $o = $this->object->find($id); $this->assertType("Page", $o); $this->assertEquals("Find test", $o->name); }
public function insert($values) { if (empty($this->table)) { throw new \Natsu\Model\Exception('Cannot insert - missing table name in model class'); } //return $this->database->lastInsertId($this->table); //die('insert'); $this->database->query("INSERT INTO [{$this->table}]", $values); return $this->database->getInsertId(); //$context = new Context($this->database); //$context->table($this->table)->insert($values); //return $context->getInsertId(); }
/** * funkce pro aktualizaci struktury * @param type $relativePath */ public function updateStructure($relativePath, $ownerId = 0) { if (empty($relativePath)) { return true; } $relativePath = \Nette\Utils\Strings::endsWith($relativePath, "/") ? substr($relativePath, 0, -1) : $relativePath; $parts = explode("/", $relativePath); $create = false; $count = count($parts); $c = 0; $parent = 0; if ($parts && $count > 0) { foreach ($parts as $part) { $folder = $this->connection->fetch("SELECT * FROM [:vd:folders] WHERE [name] = %s", $part); if (!$folder || $create) { $data = array('parent' => $parent, 'has_child' => $c < $count ? 1 : 0, 'name' => $part, 'nicename' => \Nette\Utils\Strings::webalize($part), 'editor_id' => $ownerId ?: 0); if (!empty($part)) { $this->connection->query('INSERT INTO [:vd:folders] ', $data); $parent = $this->connection->getInsertId(); } } elseif ($folder) { $parent = $folder['folder_id']; } if (!$folder || !$folder['has_child']) { $create = true; } $c++; } } return $parent; }
/** * @param object $instance * @param ClassMetadata $entityAttributes * @throws \RuntimeException * @return \DibiResult|int */ private function insertItem($instance, ClassMetadata $entityAttributes) { if ($entityAttributes->hasBeforeCreateEvent()) { $instance->beforeCreateEvent($this); } $values = $this->getInstanceValueMap($instance, $entityAttributes); $this->dibiConnection->insert($entityAttributes->getTable(), $values)->execute(); $insertId = NULL; if ($entityAttributes->getAutoIncrementFieldName()) { $insertId = $this->dibiConnection->getInsertId(); if (!$insertId) { throw new \RuntimeException('Entity has set autoIncrement flag but no incremented values was returned from DB.'); } DataHelperLoader::setPropertyValue($instance, $entityAttributes->getAutoIncrementFieldName(), $insertId); } // Unset origin class hash and set new one by primary key $hash = spl_object_hash($instance); if (array_key_exists($hash, $this->managedClasses)) { unset($this->managedClasses[$hash]); } $classKey = $this->getEntityClassHashKey($instance, $entityAttributes); $this->managedClasses[$classKey]['instance'] = $instance; $this->managedClasses[$classKey]['flag'] = self::FLAG_INSTANCE_UPDATE; $this->managedClasses[$classKey]['valueHash'] = $this->getInstanceValuesHash($instance, $entityAttributes); return $insertId; }
public function addContact($contact) { try { $this->database->query('INSERT INTO [' . self::TABLE_NAME_CONTACT . ']', [self::COLUMN_ID => $contact['id'], self::COLUMN_EMAIL => $contact['email']]); return $this->database->getInsertId(); } catch (Nette\Database\DriverException $e) { throw new \Nette\Database\DriverException(); } }
public function createInsert($table, array $values, $primaryName = null) { $query = new Query($this->connection->insert($table, $values)); $query->resultCallback = function (Query $query) { $query->fluent->execute(); return $this->connection->getInsertId(); }; return $query; }
/** * @param \obo\Entity $entity * @return void */ public function insertEntity(\obo\Entity $entity) { if ($entity->isBasedInRepository()) { throw new \obo\Exceptions\Exception("Can't insert entity into storage. Entity is already persisted."); } $this->dibiConnection->query("INSERT INTO [{$entity->entityInformation()->repositoryName}] ", $this->convertDataForImport($entity->changedProperties($entity->entityInformation()->persistablePropertiesNames, true, true), $entity->entityInformation())); if ($autoIncrementProperty = $this->informations[$entity->entityInformation()->className]["autoIncrementProperty"]) { $entity->setValueForPropertyWithName($this->dibiConnection->getInsertId(), $autoIncrementProperty); } }
/** * Generates and executes SQL query. * @param mixed what to return? * @return DibiResult|int result set object (if any) * @throws DibiException */ public function execute($return = NULL) { $res = $this->query($this->_export()); switch ($return) { case dibi::IDENTIFIER: return $this->connection->getInsertId(); case dibi::AFFECTED_ROWS: return $this->connection->getAffectedRows(); default: return $res; } }
/** * Generates and executes SQL query. * @param mixed what to return? * @return DibiResult|int result set object (if any) * @throws DibiException */ public function execute($return = NULL) { $res = $this->connection->query($this->_export()); return $return === dibi::IDENTIFIER ? $this->connection->getInsertId() : $res; }
echo "<option value='' selected></option>"; foreach ($rows as $row) { echo "<option value='" . $row['code'] . "'>" . $row['name'] . "</option>"; } break; case "savetext": $language = mysql_real_escape_string(isset($_POST['language']) ? $_POST['language'] : ''); // It will be possible to use Null Coalesce Operator in PHP 7, see: https://wiki.php.net/rfc/isset_ternary $title = mysql_real_escape_string(isset($_POST['title']) ? $_POST['title'] : ''); $text = mysql_real_escape_string(isset($_POST['text']) ? $_POST['text'] : ''); $level = mysql_real_escape_string(isset($_POST['level']) ? $_POST['level'] : ''); $source = mysql_real_escape_string(isset($_POST['source']) ? $_POST['source'] : ''); $url = mysql_real_escape_string(isset($_POST['url']) ? $_POST['url'] : ''); $public = mysql_real_escape_string(isset($_POST['pub']) ? $_POST['pub'] : ''); $db->query("INSERT INTO Texts (language,title,text,level,source,url,public) VALUES ('" . $language . "','" . $title . "','" . $text . "','" . $level . "','" . $source . "','" . $url . "','" . $public . "')"); echo $db->getInsertId(); break; case "savetest": $secret = uniqid(); $language = mysql_real_escape_string(isset($_POST['language']) ? $_POST['language'] : ''); $text = mysql_real_escape_string(isset($_POST['text']) ? $_POST['text'] : ''); $testtype = mysql_real_escape_string(isset($_POST['testtype']) ? $_POST['testtype'] : ''); $test = mysql_real_escape_string(isset($_POST['test']) ? $_POST['test'] : ''); $solution = isset($_POST['solution']) ? $_POST['solution'] : ''; $db->query("INSERT INTO Tests (secret,language,text,testtype,test,solution) VALUES ('" . $secret . "','" . $language . "','" . $text . "','" . $testtype . "','" . $test . "','" . $solution . "')"); echo $secret; break; case "sendtest": $secret = mysql_real_escape_string(isset($_POST['secretId']) ? $_POST['secretId'] : ''); $level = mysql_real_escape_string(isset($_POST['level']) ? $_POST['level'] : ''); $public = mysql_real_escape_string(isset($_POST['pub']) ? $_POST['pub'] : '');
public function getLastId() { return $this->connection->getInsertId(); }
public function insert(array $data) { $this->dibi->query('INSERT INTO %n', $this->table_name, $data); return $this->dibi->getInsertId(); }
/** * Creates IIdentity object from obtained user data * * @param mixed user data * @param IAuthenticator authenticator * * @return IIdentity */ public function createIdentity($userData, $authenticator) { $uid = NULL; $roles = array(); $profile = array(); // --------------------------------------------------------------------- // DB Password if ($authenticator instanceof Authenticators\DatabasePasswordAuthenticator) { $uid = (int) $userData->{$authenticator->getColumn($authenticator::ID)}; $profile = $userData; } elseif ($authenticator instanceof Authenticators\LdapBindAuthenticator) { $ldapData = (array) $userData; $idCol = 'id'; $tableName = 'security_users'; // LDAP Binding // DB column name -> ldap array key (or callable) $binding = array(array('username' => function ($ldapData) use($authenticator) { return mb_substr($ldapData['dn'], mb_strlen($authenticator->getQueryPrefix()), 0 - mb_strlen($authenticator->getQuerySuffix())); }), array('name' => 'givenName', 'surname' => 'sn', 'email' => function ($ldapData) use(&$binding) { $username = $binding[0]['username']($ldapData); $tokens = Strings::splitWithEscape($ldapData['dn'], ',dc='); array_shift($tokens); return $username . '@' . implode($tokens, '.'); })); // Prepare data based on LDAP binding $boundData = $this->bindValues($ldapData, $binding[0]); $this->db->query('LOCK TABLES %n WRITE', $tableName); $ds = $this->db->select('*')->from($tableName); foreach ($boundData as $key => $value) { $ds->where('%n = %s', $key, $value); } $profile = $ds->fetch(); // If profile does not exist yet if ($profile === FALSE) { $boundData = array_merge($boundData, $this->bindValues($ldapData, $binding[1])); $this->db->insert($tableName, $boundData)->execute(); $boundData[$idCol] = $uid = (int) $this->db->getInsertId(); $profile = $boundData; } else { $uid = (int) $profile[$idCol]; } $this->db->query('UNLOCK TABLES'); // TODO: configurable $groupsDn = NULL; if ($groupsDn == NULL) { $dnTokens = array_reverse($userData->getParsedDn()); foreach ($dnTokens as $k => $v) { if (!Strings::startsWith($v, 'dc=')) { array_splice($dnTokens, $k, count($dnTokens), array('ou=groups')); break; } } $groupDn = implode(array_reverse($dnTokens), ','); } $username = str_replace(array('\\', ')'), array('\\\\', '\\)'), $boundData['username']); $userGid = intval($userData->gidNumber); $filter = "(&(objectClass=posixGroup)(|(gidNumber={$userGid})(memberUid={$username})))"; $result = $authenticator->ldapConnection->search($groupsDn, $filter); foreach ($result as $record) { $roles[] = $record->cn; } } elseif ($authenticator instanceof Authenticators\DatabasePSKAuthenticator) { $uid = Strings::intoParameterizedString('psk', array($userData->key)); $roles[] = $uid; $profile = $userData; // Other authenticators } else { throw new Nette\NotSupportedException("Authenticator " . get_class($authenticator) . " not supported yet"); } // --------------------------------------------------------------------- // Remove duplicit roles $roles = array_unique($roles); // Sanity check if (!is_scalar($uid) || $uid == "") { throw new Nette\InvalidStateException("User ID has to be non-empty string or number"); } // --------------------------------------------------------------------- // Query roles from DB if it's not PSK (has user id) if (is_int($uid)) { $roles = array_merge($this->getUserRoles($uid), $roles); } // --------------------------------------------------------------------- // Identity $identity = new Nette\Security\Identity($uid, $roles, $profile); return $identity; }