在select

时间:2017-05-12 15:01:50

标签: php symfony doctrine-orm doctrine symfony-3.2

我对Symfony和Doctrine很开心,而且我遇到了多对多关系的问题。 我有接下来的两个实体,与多对多关系。

的appbundle /实体/ user.php的

/**
* @ORM\ManyToMany(targetEntity="Team", mappedBy="user", cascade={"all"})
*
*/
private $team;

public function __construct()
{
    parent::__construct();

    $this->team = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add team
 *
 * @param \AppBundle\Entity\Team $team
 *
 * @return User
 */
public function addTeam(\AppBundle\Entity\Team $team)
{
    $this->team[] = $team;

    return $this;
}

/**
 * Remove team
 *
 * @param \AppBundle\Entity\Team $team
 */
public function removeTeam(\AppBundle\Entity\Team $team)
{
    $this->team->removeElement($team);
}

/**
 * Get team
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getTeam()
{
    return $this->team;
}

的appbundle /实体/ Team.php

/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="team", cascade={"persist"})
* @ORM\JoinTable(name="user_team")
*/
private $user;

public function __construct()
{

    $this->user = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add user
 *
 * @param \AppBundle\Entity\User $user
 *
 * @return Team
 */
public function addUser(\AppBundle\Entity\User $user)
{
    $this->user[] = $user;

    return $this;
}

/**
 * Remove user
 *
 * @param \AppBundle\Entity\User $user
 */
public function removeUser(\AppBundle\Entity\User $user)
{
    $this->user->removeElement($user);
}

/**
 * Get user
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getUser()
{
    return $this->user;
}

在我的控制器中,我有以下代码:

的appbundle /控制器/ DefaultController.php

$em = $this->getDoctrine()->getManager();        
$em->getRepository('AppBundle:Team')->findBy(array('user'=>$usr));

我有下一个错误:

  

执行'SELECT t0.id AS id_1,t0.name时发生异常   AS name_2,t0.description AS description_3,t0.active AS active_4,   t0.date_add AS date_add_5,t0.date_modified AS date_modified_6 FROM   team t0 WHERE user_team.user_id =?'与params [2]:

     

SQLSTATE [42S22]:找不到列:1054未知列   'where子句'500内部服务器错误中的'user_team.user_id' -   InvalidFieldNameException

我做错了什么?

这种关系? 控制器? 也许一切?

谢谢:)

2 个答案:

答案 0 :(得分:0)

嗯,好像你忘了在这里运行doctrine:schema:update命令。

另外,请尝试修复此注释:

/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="team", cascade={"persist"})
* @ORM\JoinTable(name="user_team")
*/

类似

/**
 * @ManyToMany(targetEntity="User")
 * @JoinTable(name="User_Team",
 *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="team_id", referencedColumnName="id")}
 * )
 */

答案 1 :(得分:0)

如果您想吸引团队用户, 在Team对象上使用getUser函数:

$em    = $this->getDoctrine()->getManager();
$team  = $em->getRepository('AppBundle:Team')->find( $aTeamId );

$users = $team->getUser();