我有以下设置:
Entity: 'Customers'
Entity: 'Accounts'
这些实体属于@ManyToMany关系。
所以我有以下数据库表:
customers
accounts
customers_accounts
现在我需要为每个帐户的每个客户保存数据。例如:
有一位顾客'汤姆'。两个帐户'Ben'和'Eric'负责客户'Tom'。现在我需要保存帐户'Ben'是否已经与客户'Tom'交谈了。此外,我需要为帐户'Eric'保存相同的状态。
这里组织数据库的最佳方法是什么?最好的事情是表'customers_accounts'中有一个额外的列。那可能吗?还有哪些其他选择?
感谢您的帮助!
只是为了向您展示实体如何相互关联:
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Customer", inversedBy="accounts", cascade={"persist"})
* @ORM\JoinTable(name="customers_accounts")
**/
protected $customers;
答案 0 :(得分:1)
在ManyToMany
中,您无法在联结表中添加其他字段,即customers_accounts
,要为您的联结表添加其他字段,您必须调整映射作为创建将指向的实体您的客户和帐户实体以ManyToOne
方式,您的客户和帐户实体将以OneToMany
方式指向您的联结实体
OneToMany OneToMany
-----------> <------------
Customer CustomerHasAccounts Accounts
<---------- ------------>
ManyToOne ManyToOne
/**
* Customer
* @ORM\Table(name="customer")
* @ORM\Entity
*/
class Customer
{
/**
* @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\CustomerHasAccounts", mappedBy="customers",cascade={"persist","remove"} )
*/
protected $hasAccounts;
}
/**
* Accounts
* @ORM\Table(name="accounts")
* @ORM\Entity
*/
class Accounts
{
/**
* @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\CustomerHasAccounts", mappedBy="acccounts",cascade={"persist","remove"} )
*/
protected $hasCustomers;
}
/**
* CustomerHasAccounts
* @ORM\Table(name="customers_accounts")
* @ORM\Entity
*/
class CustomerHasAccounts
{
/**
* @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Accounts", cascade={"persist"}, fetch="LAZY")
* @ORM\JoinColumn(name="acccount_id", referencedColumnName="id")
*/
protected $acccounts;
/**
* @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Customer", cascade={"persist","remove"} , fetch="LAZY" )
* @ORM\JoinColumn(name="customers_id", referencedColumnName="id",nullable=true)
*/
protected $customers;
/**
*
* @ORM\Column(name="status", type="string")
*/
protected $status;
}
答案 1 :(得分:0)
您必须通过OneToMany-ManyToOne实体转换您的实体之间的ManyToMany关系,在您的2个ManyToMany实体之间建立链接,此实体将包含与此关系相关的所有数据。
客户
Class Customer
{
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\CustomerAccount", mappedBy="societe", cascade={"Persist"})
*/
private $customerAccounts;
}
帐户
Class Account
{
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\CustomerAccount", mappedBy="account", cascade={"Persist"})
*/
private $customerAccounts;
}
CustomerAccount
Class CustomerAccount
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Customer", inversedBy="customerAccounts")
*/
private $societe;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Account", inversedBy="customerAccounts")
*/
private $contact;
/**
* @var boolean
*
* @ORM\Column(name="alreadySpoken", type="boolean")
*/
private $alreadySpoken;
}