Symfony3 学说 在选择中找不到多对多列



我对Symfony和Doctrine很乞求,我对多对多关系有问题。我有接下来的两个实体,与多对多关系。

AppBundle/Entity/User.php

/**
* @ORMManyToMany(targetEntity="Team", mappedBy="user", cascade={"all"})
*
*/
private $team;
public function __construct()
{
    parent::__construct();
    $this->team = new DoctrineCommonCollectionsArrayCollection();
}
/**
 * Add team
 *
 * @param AppBundleEntityTeam $team
 *
 * @return User
 */
public function addTeam(AppBundleEntityTeam $team)
{
    $this->team[] = $team;
    return $this;
}
/**
 * Remove team
 *
 * @param AppBundleEntityTeam $team
 */
public function removeTeam(AppBundleEntityTeam $team)
{
    $this->team->removeElement($team);
}
/**
 * Get team
 *
 * @return DoctrineCommonCollectionsCollection
 */
public function getTeam()
{
    return $this->team;
}

AppBundle/Entity/Team.php

/**
* @ORMManyToMany(targetEntity="User", inversedBy="team", cascade={"persist"})
* @ORMJoinTable(name="user_team")
*/
private $user;
public function __construct()
{
    $this->user = new DoctrineCommonCollectionsArrayCollection();
}
/**
 * Add user
 *
 * @param AppBundleEntityUser $user
 *
 * @return Team
 */
public function addUser(AppBundleEntityUser $user)
{
    $this->user[] = $user;
    return $this;
}
/**
 * Remove user
 *
 * @param AppBundleEntityUser $user
 */
public function removeUser(AppBundleEntityUser $user)
{
    $this->user->removeElement($user);
}
/**
 * Get user
 *
 * @return DoctrineCommonCollectionsCollection
 */
public function getUser()
{
    return $this->user;
}

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

AppBundle/Controller/DefaultController.php

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

我有下一个错误:

执行"选择 t0.id AS id_1"时发生异常,t0.name AS name_2, t0.description AS description_3, t0.active AS active_4, t0.date_add date_add_5,t0.date_modified date_modified_6 团队 t0 其中 user_team.user_id = ?' 带有参数 [2]:

SQLSTATE[42S22]:找不到列:1054 未知列 "where子句"中的"user_team.user_id"500 内部服务器错误 - 无效字段名称异常

我做错了什么?

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

谢谢:)

> Mmmm 看来你忘了在这里运行doctrine:schema:update命令。

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

/**
* @ORMManyToMany(targetEntity="User", inversedBy="team", cascade={"persist"})
* @ORMJoinTable(name="user_team")
*/

到类似的东西

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

如果你想获取团队的用户,在团队对象上使用 getUser 函数:

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

相关内容

  • 没有找到相关文章