2013-12-16 34 views
-2

我是symfony2和doctrine的新手。在我的項目中,我有一個名爲clients的表,它存儲了客戶端的詳細信息.clients表中有一個名爲country id的字段,這是country表的主鍵。你可以請任何人告訴我,我必須在這種情況下設置哪種關係。如何在symfony2中設置關係

+0

你檢查http://symfony.com/doc/current/book/doctrine.html – Syjin

回答

3

看看文檔的 「Databases and Doctrine」 部分

Client>Country(多對一)

Country>Client(一對多)(如果需要)

客戶實體,

class Client 
{ 
    // ... 

    /** 
    * @ORM\ManyToOne(targetEntity="Country", inversedBy="clients") 
    * @ORM\JoinColumn(name="country_id", referencedColumnName="id") 
    */ 
    protected $country; 
} 

國家實體,

class Country 
{ 
    // ... 

    /** 
    * @ORM\OneToMany(targetEntity="Client", mappedBy="country") 
    */ 
    protected $clients; 

    public function __construct() 
    { 
     $this->clients = new ArrayCollection(); 
    } 
}