0

作为 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();
    }
4

1 回答 1

3

Container实体中,当你指定$users属性映射时, 的值mappedBy很重要:你需要在类中指定逆属性的名称User

在这里,我们有Container::$users<-> User::$access

将您的注释更改为:

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;
}

我鼓励您阅读有关关联的教义 orm 文档部分,并在您的所有关联中验证和修复此问题。

于 2012-08-18T11:00:53.980 回答