我正在使用symfony 1.4和Doctrine ORM进行开发。在构建了模式和模型之后,我得到了一些用于处理数据库的类。我也可以使用Doctrine_query。。。。我唯一不能理解的是:
我需要更新表格。
Doctrine_Query::create()->update('table')->.....->execute().
或
$tbl = new Table();
$tbl->assignIdentifier($id);
if($tbl->load()){
$tbl->setFieldname('value');
$tbl->save();
}
我如何理解它是否是查询的成功结果?以及更新了多少行。
同样的问题也适用于删除操作。
所有内容都在文档中进行更新和删除。
执行DQL UPDATE和DELETE查询时,查询的执行将返回受影响的行数。
参见示例:
$q = Doctrine_Query::create()
->update('Account')
->set('amount', 'amount + 200')
->where('id > 200');
$rows = $q->execute();
echo $rows;
$q = Doctrine_Query::create()
->delete('Account a')
->where('a.id > 3');
$rows = $q->execute();
echo $rows;
这与DQL有关(当您使用条令查询时)。但我认为->save()
会返回当前对象,或者像@PLB评论的那样返回true/false。
$statement = $this->entityManager->getConnection()->prepare($sql);
$statement->execute();
// the counter of rows which are updated
$counter = $statement->rowCount();
它与symfony 2项目中的条令2配合得很好
$nrRows = $this->getEntityManager()->getConnection()->executeUpdate('UPDATE ...');
执行更新的文档:
/**
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
* and returns the number of affected rows.
*
* This method supports PDO binding types as well as DBAL mapping types.
*
* @param string $query The SQL query.
* @param mixed[] $params The query parameters.
* @param int[]|string[] $types The parameter types.
*
* @return int The number of affected rows.
*
* @throws DBALException
*/
public function executeUpdate($query, array $params = [], array $types = [])