简体   繁体   中英

Tricky query with Doctrine querybuilder

I have a tricky query problem, I use this code to extract the containers that the user has access to (access defined in a many-to-many relation):

    //Fetch the containers
    $repository = $this->getDoctrine()->getRepository('BizTVContainerManagementBundle:Container');
    $query = $repository->createQueryBuilder('c')
        ->innerJoin('c.users','u')
        ->where('c.company = :company')
        ->setParameter('company', $companyId)
        ->orderBy('c.name', 'ASC')
        ->getQuery();
    $containers = $query->getResult();

Now I actually have a hierarchy of 4 levels of permissions - company, geoarea, building and screen. I would like to have it set up so that when a user has permissions on a parent object, the query returned the child object also, without any particular permission being set on the child object.

If I had access to symfony2's excellent entity system, I would be able to put something like

If $entity->getParent() == granted
OR
$entity->getParent()->getParent() == granted
THEN
this is granted also

but in SQL I can't dig down in levels like this, can I?

You can pull in the complete hierarchy in your query:

$query = $repository->createQueryBuilder('company')
    ->leftJoin('company.parent','geoarea')
    ->leftJoin('geoarea.parent','building')
    ->leftJoin('building.parent','screen')

It's not clear to me how you have your permissions stored but within the query you can 'or' together where conditions and check each item in the heirarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM