0

Being new to symfony2 and querybuilder, I am trying to select all my containers but limiting the result to only those for which the logged-in user has access.

With the current code I get this error:

Notice: Undefined index: Container in /var/www/biztv_symfony/vendor/doctrine/lib/Doctrine/ORM/Query/SqlWalker.php line 746

Here is my attempt at the query (I have tried a couple of options from this and other forum sites and still can't seem to understand it right...)

public function indexAction() { //Get the requisits $companyId = $this->container->get('security.context')->getToken()->getUser()->getCompany()->getId(); $em = $this->getDoctrine()->getEntityManager(); //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(); 

I keep track of access with a many-to-many relation, this is my user entity...

<?php // src/BizTV/UserBundle/Entity/User.php namespace BizTV\UserBundle\Entity; use FOS\UserBundle\Entity\User as BaseUser; use Doctrine\ORM\Mapping as ORM; use BizTV\BackendBundle\Entity\company as company; /** * @ORM\Entity * @ORM\Table(name="fos_user") */ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var object BizTV\BackendBundle\Entity\company * * @ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company") * @ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false) */ protected $company; /** * @ORM\ManyToMany(targetEntity="BizTV\ContainerManagementBundle\Entity\Container", inversedBy="User") * @ORM\JoinTable(name="access") */ private $access; public function __construct() { parent::__construct(); $this->access = new \Doctrine\Common\Collections\ArrayCollection(); } //getters and setters... 

This is my container entity:

<?php namespace BizTV\ContainerManagementBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use BizTV\UserBundle\Entity\User as user; /** * BizTV\ContainerManagementBundle\Entity\Container * * @ORM\Table(name="container") * @ORM\Entity */ class Container { /** * @var integer $id * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string $name * @Assert\NotBlank(message = "Du måste ange ett namn för området") * @ORM\Column(name="name", type="string", length=255) */ private $name; //TODO: form handler assuring no name is used twice in same company /** * @var object BizTV\BackendBundle\Entity\company * * @ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company") * @ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false) */ protected $company; /** * @ORM\ManyToMany(targetEntity="BizTV\UserBundle\Entity\User", mappedBy="Container") */ private $users; public function __construct() { $this->users = new \Doctrine\Common\Collections\ArrayCollection(); } 

1 Answer 1

3

In the Container entity, when you specify the $users property mapping, the value of mappedBy is important: you need to specify the name of the inverse property in the User class.

Here, we have, Container::$users <-> User::$access.

Change your annotations to:

class User { /** * @ORM\ManyToMany(targetEntity="BizTV\ContainerManagementBundle\Entity\Container", inversedBy="users") * @ORM\JoinTable(name="access") */ private $access; } class Container { /** * @ORM\ManyToMany(targetEntity="BizTV\UserBundle\Entity\User", mappedBy="access") */ private $users; } 

I encourage you to read the doctrine orm documentation part about associations, and verify and fix this in all your associations.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I didn't read the docs thoroughly enough, I thought the inversedBy and MappedBy were table names, not field names...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.