Skip to content

Criteria

Lucas Magroski edited this page Oct 24, 2017 · 2 revisions

Criteria

Model Criteria usage example:

class ModelCriteria extends Frogg\Model\Criteria
{
    /**
     * @return \Phalcon\Mvc\Model\Criteria
     */
    public function ofUser($id)
    {
        return $this->where('user_id = '.$id);
    }
}
$result = Model::query()->ofUser(1)->where('x' > 'y')->execute();

$firstItem = Model::query()->criteria()->findFirstById($id);

Global Criteria usage example:

class Model extends Frogg\Model
{
    /**
     * @return ModelCriteria
     */
    public static function query(DiInterface $dependencyInjector = null)
    {
        return parent::query($dependencyInjector)->addSoftDelete();
    }
}
// this result applies softDelete for default, so all 'deleted = 1' results will be filtered 
// (this filter is added at the end of the query)
$result = Model::query()->where('x' > 'y')->execute();

// but you can remove a global criteria calling removeCriteriaName
$resultWithDeleted = Model::query()->removeSoftDelete()->where('x' > 'y')->execute();
// or for this specific method, we have an alias
$resultWithDeleted = Model::query()->withDeleted()->where('x' > 'y')->execute();

Debugg tip

You can get some infos like:

$builder         = Model::query()->where('x' > 'y');
$phql            = $builder->getPhql();
$buildedQuery    = $builder->getQuery();
$criterias       = $builder->getActiveCriterias();
Clone this wiki locally