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.

Default condition for a Model

Hey guys,

any idea how to do this, perhaps in the \Phalcon\Mvc\Model::initialize(), without having to override find()/findFirst() each time? Setting some sort of default condition.

Basically, I want to keep the record in the database but hidden from the app for historical purposes only with the possibility to reintegrate back in the system easily. And I would like to avoid having to move it to some archive.

<?php

namespace Models;

use Phalcon\Mvc\Model;

class Metrics extends Model
{
    public static function find($parameters = null)
    {
        $condition = 'retired is null';

        if (is_array($parameters)) {
            if ( isset($parameters[0])) {
                $parameters[0] .= ' and '.$condition;
            }
            else {
                $parameters[0] = $condition;
            }
        }
        elseif ($parameters) {
            $parameters .= ' and '.$condition;
        }
        else {
            $parameters = $condition;
        }

        return parent::find($parameters);
    }
}

Thanks, Marek

Why not create a view in the database, and a model based on that view?

1) cant very well insert or update stuff in a view 2) would have to maintain the view as well as the table

Why not create a view in the database, and a model based on that view?

edited Oct '14

Hey man, unfortunately this wont help very well either :)

softDelete is what you want: http://docs.phalconphp.com/pt/latest/reference/models.html#softdelete

says on the website:

Note that you need to specify the deleted condition in your queries to effectively ignore them as deleted records, this behavior doesn’t support that.