请帮帮我。
我无法删除与中间表的多对多关系。
我有User,Mailing和UserMailing实体。 用户有很多邮件,邮件有很多用户。 UserMailing包含User和Mailing之间的关联
我有表格,查看所有邮件
用户实体
<?php
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="UserMailing", mappedBy="user", cascade={"all"})
* */
protected $user_mailing;
protected $mailings;
}
public function __construct()
{
parent::__construct();
$this->user_mailing = new ArrayCollection();
$this->mailings = new ArrayCollection();
}
public function getMailing()
{
$mailings = new ArrayCollection();
foreach ($this->user_mailing as $um) {
$mailings[] = $um->getMailing();
}
return $mailings;
}
public function setMailing($mailings)
{
foreach($mailings as $m)
{
$user_mailing = new UserMailing();
$user_mailing->setUser($this);
$user_mailing->setMailing($m);
$this->addUserMailing($user_mailing);
}
}
public function addUserMailing($userMailing)
{
$this->user_mailing[] = $userMailing;
}
public function removeUserMailing($userMailing)
{
$this->user_mailing->removeElement($userMailing);
}
}
邮寄实体
namespace Pet\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Pet\UserBundle\Entity\User;
/**
* @ORM\Entity
* @ORM\Table(name="mailings")
*/
class Mailing
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="UserMailing" , mappedBy="mailing" , cascade={"all"})
* */
protected $user_mailing;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this->name;
}
}
UserMailing实体
namespace Pet\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Pet\UserBundle\Entity\User;
use Pet\UserBundle\Entity\Mailing;
/**
* @ORM\Entity
* @ORM\Table(name="user_mailings")
* @ORM\HasLifecycleCallbacks()
*/
class UserMailing
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Mailing", inversedBy="user_mailing")
* @ORM\JoinColumn(name="mailing_id", referencedColumnName="id")
* */
protected $mailing;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="user_mailing")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* */
protected $user;
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
public function setMailing(Mailing $mailing = null)
{
$this->mailing = $mailing;
return $this;
}
public function getUser()
{
return $this->user;
}
public function getMailing()
{
return $this->mailing;
}
}
表格
<?php
namespace Pet\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class MailingFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('mailing', 'entity', array(
'class' => 'PetUserBundle:Mailing',
'property' => 'name',
'multiple' => true,
'expanded' => true,
'label' => 'form.mailing', 'translation_domain' => 'PetUserBundle',
));
}
public function getName()
{
return 'formMailing';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Pet\UserBundle\Entity\User'
);
}
}
控制器操作
<?php
public function showMailingAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$form = $this->container->get('form.factory')->create(new MailingFormType(), $user);
$previousCollections = $user->getMailing();
$previousCollections = $previousCollections->toArray();
$request = $this->container->get('request');
if ($request->getMethod() == 'POST')
{
$form->bind($request);
if ($form->isValid())
{
foreach($previousCollections as $um)
{
$user->removeUserMailing($um);
}
$em->persist($user);
$em->flush();
$this->setFlash('pet_user_success', 'profile.flash.updated');
return new RedirectResponse($this->getRedirectionUrl($user));
}
}
return $this->container->get('templating')->renderResponse('PetUserBundle:Profile:subscriptions.html.'.$this->container- >getParameter('fos_user.template.engine'),
array(
'form' => $form->createView(),
)
);
}
添加用户邮件 - 确定,但方法$entity->removeUserMailing($um)
不起作用((
请帮帮我......
答案 0 :(得分:0)
你的ArrayCollections搞砸了。不要自己写那些吸气剂和制定者。
删除它们,然后运行doctrine:generate:entities PetUserBundle:User
和doctrine:generate:entities PetUserBundle:Mailing
。
然后,您只需调用$user->getMailings->clear()
而不是循环遍历数组并逐个删除。