0
作爲symfony2和querybuilder的新手,我試圖選擇所有容器,但將結果限制爲只有登錄用戶纔有權訪問的容器。Symfony2如何使用querybuilder加入多對多關係?
隨着當前的代碼我得到這個錯誤:
注意:未定義指數:集裝箱/var/www/biztv_symfony/vendor/doctrine/lib/Doctrine/ORM/Query/SqlWalker.php線746
這是我在查詢嘗試(我已經嘗試了幾個選項,從這個和其他論壇網站,仍然似乎無法理解對不對...)
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();
我一直訪問的軌道上多對多的關係,這是我的用戶實體...
<?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...
這是我的容器實體:
<?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();
}
噢,我沒有徹底讀完文檔,我認爲反轉的By和MappedBy是表名,而不是字段名... – 2012-08-18 11:28:40