Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[Resolved] Autofilter softdeleted records

I looked through the first 20+ pages of discussion and performed some searching but could not find an answer.

I'm doing soft deletes on my models. I'd like to not show those deleted records by default within the model so I don't have to filter each time I query. What is the proper approach for this? Where do I specify that I want: (deleted is null) or (deleted = 0) in my query each time so that it does not prohibit me from searching/filtering within the application?

Thanks



435
Accepted
answer
edited Feb '15

I believe you would need to extend the Model class and impliment your own functions E.G.

class ExtendedModel extends \Phalcon\Mvc\Model
{
    public static function myFind($array)
    {
        $array[0] .= ' AND deleted = 0';
        return self::find($array);
    }

    //etc for findFirst, count, max
}

class Users extends ExtendedModel
{
    public $id;
    public $deleted;
}

Users::myFind([
    'id = :id',
    'bind' => ['id' => 1]
])

Interesting. I'll take this approach and see what comes of it.

Thanks!