符合条件和限制的符号 /学说



我已经创建了一个具有许多电子邮件的人模型的概念。我也有一个可以与人共享相同电子邮件的公司模型(人模型)。

简而言之:人们OneTomany ememail ememail ManutoOne电子邮件(经典的Manytomany Unirectional和Join表)

people { id, firstname, lastname, created }
people_emails { person_id, email_id, main, created }
emails { id, email, created }

以及:

companies { id, name, employee_count, created }
companies_emails { company_id, email_id, main, created }
emails { id, email, created }

问题是,我想在加入表中存储一个名为" main"的布尔值,如下所示:

person_id | email_id | main | created
    8     |     5    | true | 2014-10-21 16:54:21

...以便我可以这样做:

Mark Wilson (Person) has 5 emails.
3 of them are his company e-mails (shared): contact@company.com, office@company.com, it@company.com
2 of them are his own.
1 he answers only in his leisure time
1 is his MAIN email: i.e. m.wilson@company.com

,我想轻松地获取主要电子邮件,就像是普通人型号:

一样,我想轻松地获取主要电子邮件:
firstname: Mark (string)
lastname: Wilson (string)
emails: (array)
primary_email: (email object)

我想指出的那样,我无法将"主要"属性存储在其他任何地方,Mark和他的电子邮件之间的关系是"主要",而不是电子邮件本身。

现在,我确实存储了此值,但是问题是,如何使实体属性这样的属性:

class Person {
    (...)
    /**
     * @ORMManyToMany(targetEntity="Email")
     * @ORMJoinTable(name="people_emails",
     *      joinColumns={@ORMJoinColumn(name="person_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="email_id", referencedColumnName="id", unique=true)}
     *      )
     */
    private $primary_email;
    /**
     * @ORMManyToMany(targetEntity="Email")
     * @ORMJoinTable(name="people_emails",
     *      joinColumns={@ORMJoinColumn(name="person_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="email_id", referencedColumnName="id", unique=true)} // A condition?
     *      )
     */
    private $emails;
    public function getPrimaryEmail() {
        return $this->primary_email; // Single row limit?
    }
    public function getEmails() {
        return $this->emails;
    }
    (...)
}

问题是,我真的很想将其作为实体属性,只是为了在任何情况下使用,而无需为整个人的模型编写自定义转换功能。

,或者这是一种完全错误的方式。我想在树枝上使用该属性,例如:

{{person.primary_email.email}}

总结:我想根据可连接的专栏存储一个Manuttomany单行关系。

如何做?

有很多方法可以做到这一点,关于您的设计选择也有很多关键的事情要说。无论哪种方式,这里都是一种示例,即您可以使用两个联接表(如果要使用外键,则需要两个)。

)。
<?php
interface AddressLink
{
    public function getEmail();
    public function isMain();
}

trait EmailAddressOwner
{
    protected abstract function getAddressLinks();
    public function getPrimaryEmailAddress()
    {
        return $this->getAddressLinks()->filter(function (AddressLink $addressLink) {
            return $addressLink->isMain();
        })->first();
    }
    public function getEmailAddresses()
    {
        return $this->getAddressLinks()->map(function (AddressLink $addressLink) {
            return $addressLink->getEmail();
        });
    }
}
class PersonEmailAddress implements AddressLink
{
    private $person; // ManyToOne
    private $email; // ManyToOne
    private $main; // bool
}
class CompanyEmailAddress implements AddressLink
{
    private $company; // ManyToOne
    private $email; // ManyToOne
    private $main; // bool
}

class Email
{
    private $id;
    private $address;
}
class Person
{
    /**
     * @ORMOneToMany(targetEntity="PersonEmailAddress")
     */
    private $emailAddressLinks;
    use EmailAddressOwner;
    public function getAddressLinks()
    {
        return $this->emailAddressLinks;
    }
}
class Company
{
    /**
     * @ORMOneToMany(targetEntity="CompanyEmailAddress")
     */
    private $emailAddressLinks;
    use EmailAddressOwner;
    public function getAddressLinks()
    {
        return $this->emailAddressLinks;
    }
}

另一种方法是将一个ManyToMany与您的Email实体相关联和一个主要电子邮件地址的ManyToOne关系。

在评论中回答您是否在twig中进行

{{ person.primaryEmail.email }}

它实际上将在您的对象上调用getPrimaryEmail()方法。您可以像我上面概述的那样实施。这样,您就不需要拥有此额外的属性。

相关内容

  • 没有找到相关文章

最新更新