【发布时间】:2013-12-16 11:26:00
【问题描述】:
我是 symfony2 和教义的新手。在我的项目中,我有一个名为 clients 的表,它存储客户的详细信息。clients 表有一个名为 country id 的字段,它是 country 表的主键。你能告诉我吗那,在这种情况下我必须设置哪种关系。
【问题讨论】:
标签: php mysql symfony doctrine-orm
我是 symfony2 和教义的新手。在我的项目中,我有一个名为 clients 的表,它存储客户的详细信息。clients 表有一个名为 country id 的字段,它是 country 表的主键。你能告诉我吗那,在这种情况下我必须设置哪种关系。
【问题讨论】:
标签: php mysql symfony doctrine-orm
查看文档中的“Databases and Doctrine”部分
Client > Country(多对一)
Country > Client (OneToMany)(如果需要)
客户实体,
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();
}
}
【讨论】: