【问题标题】:Error when I try to make a ManyToMany relation in Symfony 2当我尝试在 Symfony 2 中建立多对多关系时出错
【发布时间】:2017-05-06 05:56:09
【问题描述】:

我想在组和用户之间使用 Symfony 2 在 Doctrine 中建立多对多关系:许多用户可以在多个组中,并且许多组可以有多个用户。

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

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;

但是当我在我的实体$groupe 上调用getClients() 函数时,出现以下错误:

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 表。

有人可以帮助我吗?

【问题讨论】:

  • 尝试删除文件client.php中的name="group_id"name="client_id",这样symfony可以自己生成列。

标签: symfony doctrine


【解决方案1】:

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

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_idgroupe_idclient_groupe 表的字段。使用教义命令生成Getter和setter,使用bin/console doctrine:schema:update --force命令更新数据库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-04
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    相关资源
    最近更新 更多