我有以下设置:
Entity: 'Customers'
Entity: 'Accounts'
这些实体处于@ManyToMany关系中。
所以我有以下数据库表:
customers
accounts
customers_accounts
现在,我需要为每个帐户的每个客户保存数据。例:
有一个客户"汤姆"。两个帐户"Ben"和"Eric"负责客户"Tom"。现在我需要保存帐户"Ben"是否已经与客户"Tom"交谈过。此外,我需要为帐户"埃里克"保存相同的状态。
在这里组织数据库的最佳方式是什么?最好的办法是表"customers_accounts"中有一列额外的列。这可能吗?还有哪些其他选择?
感谢您的帮助!
只是为了向您展示实体如何相互链接:
/**
* @ORMManyToMany(targetEntity="AppBundleEntityCustomer", inversedBy="accounts", cascade={"persist"})
* @ORMJoinTable(name="customers_accounts")
**/
protected $customers;
ManyToMany
您无法在联结表中添加其他字段,即customers_accounts
,要为联结表添加其他字段,您必须调整映射,因为创建一个联结实体,该实体将以ManyToOne
方式指向您的客户和帐户实体,您的客户和帐户实体将以OneToMany
方式指向您的联结实体
OneToMany OneToMany
-----------> <------------
Customer CustomerHasAccounts Accounts
<---------- ------------>
ManyToOne ManyToOne
客户实体
/**
* Customer
* @ORMTable(name="customer")
* @ORMEntity
*/
class Customer
{
/**
* @ORMOneToMany(targetEntity="NameSpaceYourBundleEntityCustomerHasAccounts", mappedBy="customers",cascade={"persist","remove"} )
*/
protected $hasAccounts;
}
帐户实体
/**
* Accounts
* @ORMTable(name="accounts")
* @ORMEntity
*/
class Accounts
{
/**
* @ORMOneToMany(targetEntity="NameSpaceYourBundleEntityCustomerHasAccounts", mappedBy="acccounts",cascade={"persist","remove"} )
*/
protected $hasCustomers;
}
客户拥有帐户实体
/**
* CustomerHasAccounts
* @ORMTable(name="customers_accounts")
* @ORMEntity
*/
class CustomerHasAccounts
{
/**
* @ORMManyToOne(targetEntity="NameSpaceYourBundleEntityAccounts", cascade={"persist"}, fetch="LAZY")
* @ORMJoinColumn(name="acccount_id", referencedColumnName="id")
*/
protected $acccounts;
/**
* @ORMManyToOne(targetEntity="NameSpaceYourBundleEntityCustomer", cascade={"persist","remove"} , fetch="LAZY" )
* @ORMJoinColumn(name="customers_id", referencedColumnName="id",nullable=true)
*/
protected $customers;
/**
*
* @ORMColumn(name="status", type="string")
*/
protected $status;
}
您必须通过 OneToMany-ManyToOne 实体在您的 2 个 ManyToMany 实体之间建立链接来转换您的实体之间的多对多关系,该实体将拥有与此关系相关的所有数据。
客户
Class Customer
{
/**
* @ORMOneToMany(targetEntity="AppBundleEntityCustomerAccount", mappedBy="societe", cascade={"Persist"})
*/
private $customerAccounts;
}
帐户
Class Account
{
/**
* @ORMOneToMany(targetEntity="AppBundleEntityCustomerAccount", mappedBy="account", cascade={"Persist"})
*/
private $customerAccounts;
}
客户帐户
Class CustomerAccount
{
/**
* @ORMId
* @ORMManyToOne(targetEntity="AppBundleEntityCustomer", inversedBy="customerAccounts")
*/
private $societe;
/**
* @ORMId
* @ORMManyToOne(targetEntity="AppBundleEntityAccount", inversedBy="customerAccounts")
*/
private $contact;
/**
* @var boolean
*
* @ORMColumn(name="alreadySpoken", type="boolean")
*/
private $alreadySpoken;
}