我有一个使用用户和帐户实体的多对多。我需要在关联表中添加一个 isOwner 字段,因此我将其更改为一对多/多对一关系。以下是我的 3 个实体(用户、帐户、用户帐户)。
当我持久化用户时,用户记录和帐户记录都添加了,但关联记录没有。我也不确定如何在保留用户的同时设置 isOwner 字段。
有谁知道如何保留关联记录?是否应该自动添加关联记录?
用户:
/**
* xxx\CoreBundle\Entity\User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User implements UserInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $firstName
*
* @ORM\Column(name="firstName", type="string", length=255, nullable=false)
* @Assert\NotBlank()
*/
private $firstName;
/**
* @var Account
*
* @ORM\OneToMany(targetEntity="UserAccount", mappedBy="user", cascade={"persist"})
*/
private $account;
public function __construct()
{
$this->account = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Add account
*
* @param xxx\CoreBundle\Entity\Account $account
*/
public function addAccount(\xxx\CoreBundle\Entity\Account $account)
{
$this->account[] = $account;
}
/**
* Get account
*
* @return Doctrine\Common\Collections\Collection
*/
public function getAccount()
{
return $this->account;
}
}
帐户:
/**
* xxx\CoreBundle\Entity\Account
*
* @ORM\Table(name="account")
* @ORM\Entity(repositoryClass="xxx\CoreBundle\Repository\AccountRepository")
*/
class Account
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
* @Assert\NotBlank()
*/
private $name;
/**
* @var User
*
* @ORM\OneToMany(targetEntity="UserAccount", mappedBy="account", cascade={"persist"})
*/
private $user;
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add user
*
* @param xxx\CoreBundle\Entity\User $user
*/
public function addUser(\xxx\CoreBundle\Entity\User $user)
{
$this->user[] = $user;
}
/**
* Get user
*
* @return Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
}
用户帐号:
/**
* xxx\CoreBundle\Entity\UserAccount
*
* @ORM\Table(name="user_account")
* @ORM\Entity
*/
class UserAccount
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="account", cascade={"persist"})
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="Account", inversedBy="user", cascade={"persist"})
*/
private $account;
/**
* @var boolean $isOwner
*
* @ORM\Column(name="isOwner", type="boolean", nullable=false)
*/
private $isOwner;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get isOwner
*
* @return bool
*/
public function getIsOwner()
{
return $this->isOwner;
}
/**
* Set isOwner
*
* @param string $isOwner
*/
public function setIsOwner($isOwner)
{
$this->isOwner = $isOwner;
}
/**
* Set user
*
* @param xxx\CoreBundle\Entity\User $user
*/
public function setUser(\xxx\CoreBundle\Entity\User $user)
{
$this->user = $user;
}
/**
* Get user
*
* @return xxx\CoreBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set account
*
* @param xxx\CoreBundle\Entity\Account $account
*/
public function setAccount(\xxx\CoreBundle\Entity\Account $account)
{
$this->account = $account;
}
/**
* Get account
*
* @return xxx\CoreBundle\Entity\Account
*/
public function getAccount()
{
return $this->account;
}
}
控制器:
$account = new Account();
$account->setName('Test account');
$user = new User();
$user->setFirstName('John');
$user->addAccount($account);
$manager->persist($user);
$manager->flush();