在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 实体
/**
* 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;
}