/** * Sets the correct id on the insert query. * * @param ezcPersistentObjectDefinition $def * @param ezcDbHandler $db * @param ezcQueryInsert $q * @return void */ public function preSave(ezcPersistentObjectDefinition $def, ezcDbHandler $db, ezcQueryInsert $q) { // Sanity check. // ID must have been stored during the persistence check before inserting the object. if ($this->id === null) { throw new ezcPersistentIdentifierGenerationException($def->class, 'ezcPersistentManualGenerator expects the ID to be present before saving.'); } $q->set($db->quoteIdentifier($def->idProperty->columnName), $q->bindValue($this->id, null, $def->idProperty->databaseType)); }
/** * Bind all non-id properties to the given query $q. * * Binds all property values contained in $state to the given query $q, as * defined by $def. * * @param ezcQueryUpdate|ezcQueryInsert $q * @param ezcPersistentObjectDefinition $def * @param array $state * @return ezcQueryUpdate|ezcQueryInsert */ private function bindNonIdPropertyValuesToQuery($q, ezcPersistentObjectDefinition $def, array $state) { foreach ($state as $name => $value) { if ($name !== $def->idProperty->propertyName) { // Set each of the properties. $q->set($this->database->quoteIdentifier($def->properties[$name]->columnName), $q->bindValue($value, null, $def->properties[$name]->databaseType)); } } }
public function testOnDatabaseWithWhere() { // fill database with some dummy data $q = new ezcQueryInsert(ezcDbInstance::get()); // insert some data we can update $company = 'eZ systems'; $section = 'Norway'; $q->insertInto('query_test')->set('id', 1)->set('company', $q->bindParam($company))->set('section', $q->bindParam($section))->set('employees', 20); $stmt = $q->prepare(); $stmt->execute(); $q->insertInto('query_test'); $q->set('id', 2); $q->set('employees', 70); $company = 'trolltech'; $section = 'Norway'; $stmt = $q->prepare(); $stmt->execute(); // delete one $this->q->deleteFrom('query_test')->where($this->q->expr->eq('id', 1)); $stmt = $this->q->prepare(); $stmt->execute(); // test that table has one row $db = ezcDbInstance::get(); $q = $db->createSelectQuery(); // get select query $q->select('*')->from('query_test'); $stmt = $q->prepare(); $stmt->execute(); $result = $stmt->fetchAll(); $this->assertEquals(1, count($result)); }
public function testOnDatabaseWithWhere() { $q = new ezcQueryInsert(ezcDbInstance::get()); $company = 'eZ systems'; $section = 'Norway'; // insert some data we can update $q->insertInto('query_test')->set('id', 1)->set('company', $q->bindParam($company))->set('section', $q->bindParam($section))->set('employees', 20); $stmt = $q->prepare(); $stmt->execute(); $q->insertInto('query_test'); $q->set('id', 2); $q->set('employees', 70); $company = 'trolltech'; $section = 'Norway'; $stmt = $q->prepare(); $stmt->execute(); $this->q->update('query_test')->set('employees', 50)->where($this->q->expr->eq('id', 1)); $stmt = $this->q->prepare(); $stmt->execute(); // check that entry 1 was updated correctly // but not two $db = ezcDbInstance::get(); $q = $db->createSelectQuery(); // get select query $q->select('*')->from('query_test')->orderBy('company'); $stmt = $q->prepare(); $stmt->execute(); $result = $stmt->fetchAll(); $this->assertEquals(50, (int) $result[0][3]); $this->assertEquals(70, (int) $result[1][3]); }