2016-12-20 3 views
0

symfony 2でグループとユーザーの間でManyToMany関係を作成したいと思っています。多くのユーザーが多数のグループに属していて、Symfony 2でManyToMany関係を作成しようとするとエラーが発生する

その後、私のエンティティで、私はこれを行う:

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、次のエラーが発生しました:

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テーブルは追加されません。

誰かが私を助けることができますか?

+0

ファイルclient.phpで 'name =" group_id "'と 'name =" client_id "を削除してみてください。そうすればsymfonyはその列自体を生成できます。 – Preciel

答えて

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テーブルのフィールドです。 bin/console doctrine:schema:update --forceコマンドを使用して、doctrineコマンドとデータベースを使用してゲッターとセッターを生成します。

関連する問題