gpt4 book ai didi

php - 选择时找不到 Symfony3 Doctrine 多对多列

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:39 26 4
gpt4 key购买 nike

我是 Symfony 和 Doctrine 的初学者,我对多对多关系有疑问。我有接下来的两个实体,与多对多相关。

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

在我的 Controller 中,我有以下代码:

AppBundle/Controller/DefaultController.php

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

我有下一个错误:

An exception occurred while executing '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 = ?' with params [2]:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_team.user_id' in 'where clause' 500 Internal Server Error - InvalidFieldNameException

我做错了什么?

关系? Controller ?也许一切?

谢谢你:)

最佳答案

嗯,看来你忘了在这里运行 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com