We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to check if UPDATE was successful

How to check if UPDATE statement was successful??? I can't find it https://docs.phalcon.io/en/latest/reference/phql.html

$success = $query->execute();
if ($success) {
    $this->flash->success("ok");
}

is always returning true

$result = $query->execute();

$result->success()

is always returning "1"



7.9k
edited Mar '14

that suppose to be a successful update,

check in your database that records are correctly updated, or query again the record and try to create a fail by providing wrong values for the fields to see what is the $success in that case

edited Mar '14

maybe this will work:

$success = $query->execute();
if ($success != false) {
    $this->flash->success("ok");
}

Try this:

$phql = "UPDATE Cars SET color='red' WHERE id=1";
$result = $this->modelsManager->executeQuery($pqhl);
if($result->success() == true)
{
    //do something
}


29.8k
edited Mar '14

The problem is that $result->success() is always == 1 and $success is always != false even when UPDATE is not successful



98.9k

When does the update is not successful for you?



29.8k
edited Mar '14
$query = $this->modelsManager->createQuery(
                "UPDATE Users SET email_code = 0, password = :password: WHERE email = :email: AND email_code = :email_code:");

$result = $query->execute(array(
    'pass' => $password,
    'email' => $email,
    'email_code' => $email_code
));

$email and $email_code are (parameters) from url.

$result->success() is always ==1 even when UPDATE really take place and when parameters are wrong.



98.9k

Why it is not successful? It's successful if the validators defined on the model Users do not reject the values used as parameters.



29.8k

So query which is not changing (updating) anything is successful too????(wrong parameters)



29.8k
edited Mar '14
public function someAction($email)
{
        $query = $this->modelsManager->createQuery(
            "UPDATE Users SET name = 'NameAfterUpdate' WHERE email = :email:");

        $result = $query->execute(array(
            'email' => $email
        ));

        if ($result->success()==true) {
               $this->flash->success("ok");
         }
         else {
               $this->flash->error("error");
         }
 }

$result->success() always return true. I've got Phalcon\Mvc\Model\Validator\Email validation in model Users and even when $email = 'aaaaaaaaaa' $result->success()==true

edited Mar '14

If you can get DB connection try this after running execute()

$connection->affectedRows();

You can find more in documentation: https://docs.phalcon.io/en/latest/api/Phalcon_Db_Adapter_Pdo.html



98.9k
edited Mar '14

So, according to your expected behavior this must work:

$pdo = new PDO ("mysql:host=localhost;dbname=test","root","");

//Delete all records
$pdo->exec("DELETE FROM users");

//SQL is valid but no records to update
if (!$pdo->exec("UPDATE users SET name = 'some name'")) {
    echo "no records to update or an error has occurred";
}
edited May '14

I am looking for an answer to a similar issue. Is there no way to get the affected rows using ORM/PHQL on an UPDATE/DELETE? Do I have to resort to Raw SQL to get the affected rows?

I am using query method for UDPATE/DELETE using Raw SQL. Phalcon docs suggests using execute method for UPDATE/DELETE. The following works.

$result = $connection->query($updateSql, $params);

if ($result->numRows()) {
    //do something on success
}

Resulting object belongs to \Phalcon\Db\Result\Pdo.



5.7k

I second the problems described above. It would be great if the PDO statements themselves would indicate whether a query is succesful or not, by means of a BOOLEAN.

E.g. this is my code:

$del_cust_recipe = $this->connection->delete('custom_recipes','owner_id=:owner_id AND recipe_id=:recipe_id',array('owner_id'=>$this->user_id,'recipe_id'=>$recipe_id));

$del_cust_recipe always returns true here, even though no rows were deleted....

I have similar issue with update and delete query