我对symfony和教义很感兴趣,我对多对多的关系有问题。
我有接下来的两个实体,与多对多的关系。
appbundle/entity/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/entity/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/controller/defaultcontroller.php
$em = $this->getDoctrine()->getManager();
$em->getRepository('AppBundle:Team')->findBy(array('user'=>$usr));

我有下一个错误:
执行'select t0.id as id_1,t0.name'时发生异常
如名称2,t0。说明为说明3,t0。激活为激活4,
t0.date_add as date_add_5,t0.date_modified as date_modified_6开始
team t0其中user_team.user_id=?'带参数[2]:
sqlstate[42s22]:未找到列:1054未知列
“where子句”500内部服务器错误中的“user_team.user_id”-
无效字段名称异常
我做错什么了?
关系?
控制器?
也许所有的事?
谢谢:)

最佳答案

嗯,你好像忘记在这里运行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")}
 * )
 */

关于php - 在选择中找不到Symfony3 Doctrine “多对多”列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43940877/

10-14 03:11