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

SQL filter

Hi, I use the column called active for deleted records and use phalcon feature softDelete to mark deleted records. But I would like to add some SQL filter to avoid read deleted records from database for all entites which implement softdelete like in doctrine Doctrine SQL filter. Is it posible in Phalcon? Thank you.

Hey, i did not find and dont know of something similar to doctrine way (someone correct me pelase if im wrong).

However you can simply use deleted=0 in your where clause.

Phalcon provides filter functionality which is useful when "filter criteria" is not offered by the db. Example: https://docs.phalcon.io/en/latest/reference/models.html#filtering-resultsets



7.5k

Yes, now I write active = 1 in where clause to each SQL query. Filtering resultsets works but it doesn't work on SQL level.

Hey, i did not find and dont know of something similar to doctrine way (someone correct me pelase if im wrong).

However you can simply use deleted=0 in your where clause.

Phalcon provides filter functionality which is useful when "filter criteria" is not offered by the db. Example: https://docs.phalcon.io/en/latest/reference/models.html#filtering-resultsets



85.5k
edited Nov '15

i would do it :


class MyModel extends Model {

    //bla bla bla

    public static function getActivItems(){
        return self::find([
            'conditions' => 'active == ?1',
            'bind' => [1 => 1]
        ]);
    }

    //or you can do it more "dynamic"
    public static function find($parameters = array()){

        if (!isset($parameters['conditions']){
            $parameters['conditions'] = '';
        }
        $parameters['conditions'] = $parameters['conditions'] . ' active = :active: ';
        $parameters['bind']['active'] = 1;
        return parent::find($parameters);
    }

}
  1. $model = MyModel::getActivItems();

  2. $model = MyModel::find();

or someting like that, i hope you get the idea



7.5k

Thanks, I'am thinking about 'dynamic' solution, but it's only for \Phalcon\Model methods (find, findFirst, count, getRelated etc.)For query builder and PHQL programmer can't forget to add condition to the query.