7

I am developing with symfony 1.4 and using Doctrine ORM. After building schema and models i've got some classes for work with database. I can also use Doctrine_query .... The only thing, that i cann`t understend is:

I need to update table.

Doctrine_Query::create()->update('table')->.....->execute().

or

$tbl = new Table();
$tbl->assignIdentifier($id);
if($tbl->load()){
    $tbl->setFieldname('value');
    $tbl->save();
}

how can i understend was it successful result of query or not ? and how much rows was updated.

p.s. the same question is for delete operation.

1
  • 1
    Most functions like save(), delete and so on of most ORM-s will return true on success and false on failure.
    – Leri
    Jul 24, 2012 at 9:15

3 Answers 3

13

Everything is in the doc for update and delete.

When executing DQL UPDATE and DELETE queries the executing of a query returns the number of affected rows.

See examples:

$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;

This is related to DQL (when you are using doctrine queries). But I think ->save() will return the current object or true/false as @PLB commented.

7
$statement = $this->entityManager->getConnection()->prepare($sql);
$statement->execute();

// the counter of rows which are updated
$counter = $statement->rowCount();

It works for me very well with doctrine 2 in symfony 2 project

0
0
$nrRows = $this->getEntityManager()->getConnection()->executeUpdate('UPDATE ...');

The documentation for executeUpdate:

  /**
     * 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 = [])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.