当我尝试在Symfony

当我尝试在Symfony

本文介绍了当我尝试在Symfony 2中创建ManyToMany关系时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个组和一个用户之间的Symfony 2中在Doctrine中建立ManyToMany关系:许多用户可以在许多组中,许多组可以有很多用户。

I want to make a ManyToMany relation in Doctrine with Symfony 2 between a group and an user : many users can be in many groups and many groups can have many users.

然后在我的实体中,我这样做:

Then in my entities, i do this :

Groupe.php

/**
* Many Groups have Many clients.
* @ORM\ManyToMany(targetEntity="Utilisateurs\UtilisateursBundle\Entity\Client", mappedBy="groupe")
* @ORM\JoinTable(name="client_groupe")
*/
private $clients;

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

Client.php

/**
     * @ORM\ManyToMany(targetEntity="Ecommerce\EcommerceBundle\Entity\Groupe", inversedBy="clients")
     * @ORM\JoinTable(name="client_groupe",
     *   joinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")},
     *   inverseJoinColumns={@ORM\JoinColumn(name="groupe_id", referencedColumnName="id")}
     * )

     */
    private $groupe;

但是当我调用 getClients()在我的实体上的功能 $ groupe ,发生以下错误:

but when I call the getClients() function on my entity $groupe, following error occured :

FROM client t0 WHERE client_groupe.groupe_id = ?' with params ["2"]:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'client_groupe.groupe_id' in 'where clause'

它不会在from子句中添加 client_groupe

It doesn't add the client_groupe table in the from clause.

有人可以帮助我吗?

推荐答案

不需要添加table from from子句中的 client_groupe 尝试从 Groupe.php 中删除​​ * @ ORM\JoinTable(name =client_groupe)。看看 ManyToMany 关系场景的以下工作示例。

No need to add client_groupe table in from clause. Try after removing * @ORM\JoinTable(name="client_groupe") from Groupe.php. Have a look at following working example of ManyToMany relationship scenario.

Groupe.php

/**
* Many Groups have Many clients
* @ORM\ManyToMany(targetEntity="Utilisateurs\UtilisateursBundle\Entity\Client", mappedBy="groupe")
*/
private $clients;

Client.php

/**
 * @ORM\ManyToMany(targetEntity="Ecommerce\EcommerceBundle\Entity\Groupe", inversedBy="clients")
 * @ORM\JoinTable(name="client_groupe",
 *   joinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")},
 *   inverseJoinColumns={@ORM\JoinColumn(name="groupe_id", referencedColumnName="id")}
 * )
 */
private $groupe;

client_id groupe_id client_groupe 表的字段。使用doctrine命令生成Getter和setter,并使用 bin / console doctrine:schema:update --force 命令更新数据库。

client_id and groupe_id is fields of client_groupe table. Generate Getter and setter using doctrine command and update database using bin/console doctrine:schema:update --force command.

这篇关于当我尝试在Symfony 2中创建ManyToMany关系时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:50