Filtering queries based on the current user state I have a Symfony2 project. I have an Entity Asset which can have relations with Category. I store a categoryCount-field within the Asset to determine if an asset has one, to select those fast via a DQL-query depending on the current user state.

Now found myself writing something along the lines of this in my AssetRepository:

/**
 * @param string $orderBy
 * @param int $mode
 *
 * @return \Doctrine\ORM\Query
 */
public function getQueryForAll($orderBy = 'DESC', $mode = self::FILTER_WITHOUT_QUARANTINED)
{
    $qb = $this-&gt;createQueryBuilder('asset');
    $qb-&gt;orderBy('asset.updatedAt', $orderBy);

    $filter = $this-&gt;getQuarantinedCriteria($mode);

    if ($filter) {
        $qb-&gt;addCriteria($filter);
    }

    return $query = $qb-&gt;getQuery();
}

/**
 * @param int $mode
 *
 * @return Criteria|null
 */

protected function getQuarantinedCriteria($mode = self::FILTER_WITHOUT_QUARANTINED) {
    $filter = null;

    if ($mode === self::FILTER_WITHOUT_QUARANTINED) {
        $filter = Criteria::create()
            -&gt;andWhere(Criteria::expr()-&gt;gt('categoryCount', 0));
    } else if ($mode === self::FILTER_ONLY_QURANTINED) {
        $filter = Criteria::create()
            -&gt;andWhere(Criteria::expr()-&gt;eq('categoryCount', 0));
    }

    return $filter;
}


This doesn't feel right to me. The $mode flag triggers my code smell alarm, I also am worrying about passing it into each asset querying method.

Furthermore, since I will have to set the mode depending on the state of the current logged in user, I also am worried that I will produce code duplications by doing a lot of if-user-has-the-rights-checks in my actions or services:

/**
 * @Route("/all_assets/{page}",
 *         name="get_paginated_assets",
 *         requirements={"page" = "\d+"},
 *         defaults={"page" = "1"},
 *         options={"expose"=true}
 * )
 */

public function getPaginatedAssetActions($page)
{
    $repo = $this-&gt;getAssetRepository();

    $isAllowedToViewRestrictedAssets =  $this-&gt;getCurrentUser()-&gt;isAdmin();

    if ($isAllowedToViewRestrictedAssets) {
        $mode = Entity\AssetRepository::FILTER_SHOW_ALL;
    }

    $query = $repo-&gt;getQueryForAll($mode);

    $pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
    $data = $this-&gt;getCurrentPagedResults($pagerfanta, $page);

    $view = $this-&gt;view();
    $view-&gt;setFormat('json');
    $view-&gt;setData($data);

    return $view;
}


Is this the right way to implement the logic to enable filters in a Repository? Feedback on whether there is a more decoupled way would be highly appreciated.
You can try to use SQLFilter for this purpose. It allows you to tune SQL query based on some logic. You can pass information about your user's state into filter through DI container and use this information to tune your SQL query. Please note that unlike repositories - SQLFilter works on SQL level, not on DQL. More information can be found into Doctrine's documentation.