2013-12-16 109 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(); 
    } 
}