【问题标题】:How to set relationships in symfony2 [closed]如何在 symfony2 中设置关系 [关闭]
【发布时间】:2013-12-16 11:26:00
【问题描述】:

我是 symfony2 和教义的新手。在我的项目中,我有一个名为 clients 的表,它存储客户的详细信息。clients 表有一个名为 country id 的字段,它是 country 表的主键。你能告诉我吗那,在这种情况下我必须设置哪种关系。

【问题讨论】:

标签: php mysql symfony doctrine-orm


【解决方案1】:

查看文档中的“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();
    }
}

【讨论】:

    猜你喜欢
    • 2017-01-22
    • 2021-06-23
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2011-09-03
    相关资源
    最近更新 更多