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

Is really necessary to find rows before delete?

Hi there. Is there a way to delete a row without using find() before?

This is how it's explained on docs:

<?php
foreach (Robots::find("type='mechanical'") as $robot) {
    if ($robot->delete() == false) {
        echo "Sorry, we can't delete the robot right now: \n";
        foreach ($robot->getMessages() as $message) {
            echo $message, "\n";
        }
    } else {
        echo "The robot was deleted successfully!";
    }
}

I'd like to just delete the row, like this, to avoid unnecessary queries on database:

Robots:delete( $id );

Thanks in advance!

edited Apr '15

Phalcon ORM is an object relational mapper which implements ActiveRecord, the pattern you are expecting is a table data gateway that the ORM does not implement

Hi,

you can write your own delete metod. Something like this:

class ModelBase extends \Phalcon\Mvc\Model {

    public static function directDelete($id) {

        // some awesome validation

        // awesome generating pure sql

        // run sql

        // catch and return possible errors
    }

}

But keep in mind, that delete metod in phalcon is doing lot of stuff https://github.com/phalcon/cphalcon/blob/1.3.0/ext/mvc/model.c#L4724, so it is not good idea override it. ;)

And if you write about unnecessary queries on database, take a look on Model->save() metod. Phalcon alway use one select before it, and then make create or update. So i think it is better use Model->create() or Model->update(). Next benefit is, that you dont create new record instead updating one, if you miss id or something go wrong.

Ok guys, thanks for the replies!