Constrain

Every query generated for the entity selection (directly or via relation) might pass through the defined constraint. Constraints are used to define global query limits or/and filter entity by one of its relations.

Screenshot_76

In some cases, you can disable constrain usage on root query to get access to unfiltered entities. Use $select->constrain(null) to do that.

Example

A simple example can demonstrate how to only select entities which are not marked as deleted:

class NotDeletedConstrain implements ConstrainInterface
{
    public function apply(QueryBuilder $query)
    {
        $query->where('deleted_at', '=', null);
    }
}

Constrain can be assigned to any entity via schema, in case of annotated extension:

/**
 * @Entity(constrain="NotDeletedConstrain")
 */
class User
{
    // ...
}
Edit this page