3

I'm about to output a list that includes several documents(called waiver). However not every user should be allowed to see all documents and therefore I've implemented an filter to check if the user has the same "airline" and "market" assigned. So every user should only see the documents that are assigned to his "airline" and "market". This is f.e. the getter for the airline of the user entity:

 /**
 * Get airlines
 *
 * @return array 
 */
public function getAirlines()
{
    if($this->airlines != null)
    {
        $airlines = explode(",", $this->airlines);
        return $airlines; 
    }

    return Array();        
}

This is the controller logic:

        //Get User:
        $user = $this->container->get('security.context')->getToken()->getUser();

        // Gets an Array of User markets
        $user_markets = $user->getMarkets();

        // Gets an Array of User carriers
        $user_airlines = $user->getAirlines();


        if(!$this->ROLE_IS(Array( 'ROLE_XY'))){
            $query = $em->createQuery(
                'SELECT w
                FROM WaiverBundle:Waiver w
                WHERE w.carrier = :carrier 
                AND w.market = :market
                ORDER BY w.id DESC'
            )
               ->setFirstResult($page*10-10)
               ->setMaxResults(10)
               // I wan't to get the whole array and not just one position here:
               ->setParameters(array(':carrier'=>$user_airlines[0],
                ':market'=>$user_markets[0],
            ));  
        }else{
            $query = $em->createQuery(
                'SELECT u
                FROM WaiverBundle:Waiver u
                ORDER BY u.id DESC'
            )
               ->setFirstResult($page*10-10)
               ->setMaxResults(10);
        }

Question: How do I manage to compare the DQL attributes with an array and not just a string as a parameter?

2 Answers 2

4

I think you want to use "IN" syntax, not "=" syntax:

'SELECT w
    FROM WaiverBundle:Waiver w
    WHERE w.carrier IN (:carrier)
    AND w.market = :market
    ORDER BY w.id DESC'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I didn't know that there existed a special parameter ("IN") for this problem. This seems to solve my issue.
Awesome, then please upvote and accept my answer ;-)
3

Your query is not complicated. I think you should consider QueryBuilder instead of DQL in this case. Something like this would do the trick:

    $qb = $em->createQueryBuilder();
    $qb->select('w')
        ->from('WaiverBundle:Waiver', 'w')
        ->where($qb->expr()->in('w.carrier', ':carrier'))
        ->andWhere($qb->expr()->eq('w.market', ':market'))
        ->orderBy('w.id', 'DESC')
        ->setParameters(
            array(
                'carrier'=>$user_airlines[0],
                'market'=>$user_markets[0)
        );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.