Doctrine orm symfony 3无法持久化阵列从相反方向进行多对多集合
我有3个实体,军队,武器和单位。对于陆军和部队,我没有任何问题,但当我坚持使用武器时,它是陆军和部队条令的反面,除了阵列集合中的数据之外,它保存了数据库中的所有数据 我在控制器中完成了dump$wearm,并且使用我分配的所有元素正确创建了对象 实体 武器反面和阵列收集不保存的一面Doctrine orm symfony 3无法持久化阵列从相反方向进行多对多集合,doctrine-orm,many-to-many,symfony-3.3,arraycollection,Doctrine Orm,Many To Many,Symfony 3.3,Arraycollection,我有3个实体,军队,武器和单位。对于陆军和部队,我没有任何问题,但当我坚持使用武器时,它是陆军和部队条令的反面,除了阵列集合中的数据之外,它保存了数据库中的所有数据 我在控制器中完成了dump$wearm,并且使用我分配的所有元素正确创建了对象 实体 武器反面和阵列收集不保存的一面 <?php namespace ArmyDataBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\C
<?php
namespace ArmyDataBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Weapon
*
* @ORM\Table(name="weapon")
*@ORM\Entity(repositoryClass="ArmyDataBundle\Repository\WeaponRepository")
*/
class Weapon
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var int
*
* @ORM\Column(name="distance", type="integer")
*/
private $distance;
/**
* @var int
*
* @ORM\Column(name="f", type="integer")
*/
private $f;
/**
* @var int
*
* @ORM\Column(name="fp", type="integer")
*/
private $fp;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* @var int
*
* @ORM\Column(name="shoots", type="integer")
*/
private $shoots;
/**
* Many Weapons have many Armies
* @ORM\ManyToMany(targetEntity="ArmyDataBundle\Entity\Army", mappedBy="weapons")
*/
private $armies;
/**
* Many Weapons have many Units
* @ORM\ManyToMany(targetEntity="Unit", mappedBy="weapons")
*/
private $units;
public function __construct()
{
$this->armies = new \Doctrine\Common\Collections\ArrayCollection();
$this->units = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Weapon
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set distance
*
* @param integer $distance
*
* @return Weapon
*/
public function setDistance($distance)
{
$this->distance = $distance;
return $this;
}
/**
* Get distance
*
* @return int
*/
public function getDistance()
{
return $this->distance;
}
/**
* Set f
*
* @param integer $f
*
* @return Weapon
*/
public function setF($f)
{
$this->f = $f;
return $this;
}
/**
* Get f
*
* @return int
*/
public function getF()
{
return $this->f;
}
/**
* Set fp
*
* @param integer $fp
*
* @return Weapon
*/
public function setFp($fp)
{
$this->fp = $fp;
return $this;
}
/**
* Get fp
*
* @return int
*/
public function getFp()
{
return $this->fp;
}
/**
* Set type
*
* @param string $type
*
* @return Weapon
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @return int
*/
public function getShoots()
{
return $this->shoots;
}
/**
* @param int $shoots
*/
public function setShoots($shoots)
{
$this->shoots = $shoots;
}
/**
* @return mixed
*/
public function getArmies()
{
return $this->armies;
}
/**
* @param mixed $armies
*/
public function setArmies($armies)
{
$this->armies = $armies;
}
/**
* @param mixed $units
*/
public function setUnits($units)
{
$this->units = $units;
}
/**
* Add army
*
* @param $army
*
* @return Weapon
*/
public function addArmies( \ArmyDataBundle\Entity\Army $army)
{
if (!$this->armies->contains($army)){
$army->addWeapons($this);
$this->armies->add($army);
}
return $this;
}
/**
* Remove army
*
* @param $army
*/
public function removeArmies( \ArmyDataBundle\Entity\Army $army)
{
$this->armies->removeElement($army);
}
/**
*
*/
public function clearArmies(){
$this->armies->clear();
}
/**
* @return mixed
*/
public function getUnits()
{
return $this->units;
}
/**
* Add unit
*
* @param $unit
*
* @return Weapon
*/
public function addUnits( \ArmyDataBundle\Entity\Unit $unit)
{
if (!$this->units->contains($unit)){
$this->units = $unit;
$unit->addWeapons($this);
}
return $this;
}
/**
* Remove unit
*
* @param $unit
*/
public function removeUnits( \ArmyDataBundle\Entity\Unit $unit)
{
$this->units->removeElement($unit);
}
/**
*
*/
public function clearUnits(){
$this->units->clear();
}
public function __toString()
{
return (String)$this->name;
}
}
军队
单位
这是控制器中的方法
public function addAction(Request $request)
{
$weapon = new Weapon();
$form = $this->createForm('ArmyDataBundle\Form\WeaponType' , $weapon);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$units = $weapon->getUnits();
$em = $this->getDoctrine()->getManager();
$em->persist($weapon);
$em->flush($weapon);
return $this->redirectToRoute('wp_index');
}
return $this->render('@ArmyData/Weapon/add_wp.html.twig', array(
'weapon' => $weapon,
'form' => $form->createView(),
));
}
<?php
namespace ArmyDataBundle\Form;
use ArmyDataBundle\Entity\Army;
use ArmyDataBundle\Entity\Unit;
use ArmyDataBundle\Entity\Weapon;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class WeaponType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('label' => 'weapon.nam'))
->add('distance', IntegerType::class, array('label' => 'weapon.ds'))
->add('f', ChoiceType::class, array('choices' => range(0, 10)))
->add('fp', ChoiceType::class, array('choices' => range(0, 10)))
->add('type', ChoiceType::class, array('choices' => array(
'weapon.as' => 'Asalto',
'weapon.ps' => 'Pesada',
'weapon.rf' => 'Fuego Rapido',
'weapon.ar' => 'Artilleria',
),'label'=> 'weapon.tp'
))
->add('shoots', IntegerType::class, array (
'label'=> 'weapon.st'
))
->add('armies', CollectionType::class, [
'entry_type' => EntityType::class,
'entry_options' => [
'class' => Army::class
],
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'allow_extra_fields' => true,
'by_reference' => false,
])
->add('units', CollectionType::class, [
'entry_type' => EntityType::class,
'entry_options' => [
'class' => Unit::class
],
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'allow_extra_fields' => true,
'by_reference' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ArmyDataBundle\Entity\Weapon' ,
));
}
public function getBlockPrefix()
{
return 'weapon';
}
}
整个项目正在进行中
谢谢您的关注。好的,我终于解决了,我不知道这是否是最好的方法,但我修改了控制器,现在可以工作了
public function addAction(Request $request)
{
$weapon = new Weapon();
$form = $this->createForm('ArmyDataBundle\Form\WeaponType' , $weapon);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$units = $weapon->getUnits();
$armies = $weapon->getArmies();
$em = $this->getDoctrine()->getManager();
foreach ($armies as $army){
$army->addWeapons($weapon);
$em->persist($army);
$em->flush($army);
}
foreach ($units as $unit){
$unit->addWeapons($weapon);
$em->persist($unit);
$em->flush($unit);
}
$em->persist($weapon);
$em->flush($weapon);
return $this->redirectToRoute('wp_index');
}
public function addAction(Request $request)
{
$weapon = new Weapon();
$form = $this->createForm('ArmyDataBundle\Form\WeaponType' , $weapon);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$units = $weapon->getUnits();
$em = $this->getDoctrine()->getManager();
$em->persist($weapon);
$em->flush($weapon);
return $this->redirectToRoute('wp_index');
}
return $this->render('@ArmyData/Weapon/add_wp.html.twig', array(
'weapon' => $weapon,
'form' => $form->createView(),
));
}
<?php
namespace ArmyDataBundle\Form;
use ArmyDataBundle\Entity\Army;
use ArmyDataBundle\Entity\Unit;
use ArmyDataBundle\Entity\Weapon;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class WeaponType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('label' => 'weapon.nam'))
->add('distance', IntegerType::class, array('label' => 'weapon.ds'))
->add('f', ChoiceType::class, array('choices' => range(0, 10)))
->add('fp', ChoiceType::class, array('choices' => range(0, 10)))
->add('type', ChoiceType::class, array('choices' => array(
'weapon.as' => 'Asalto',
'weapon.ps' => 'Pesada',
'weapon.rf' => 'Fuego Rapido',
'weapon.ar' => 'Artilleria',
),'label'=> 'weapon.tp'
))
->add('shoots', IntegerType::class, array (
'label'=> 'weapon.st'
))
->add('armies', CollectionType::class, [
'entry_type' => EntityType::class,
'entry_options' => [
'class' => Army::class
],
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'allow_extra_fields' => true,
'by_reference' => false,
])
->add('units', CollectionType::class, [
'entry_type' => EntityType::class,
'entry_options' => [
'class' => Unit::class
],
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'allow_extra_fields' => true,
'by_reference' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ArmyDataBundle\Entity\Weapon' ,
));
}
public function getBlockPrefix()
{
return 'weapon';
}
}
public function addAction(Request $request)
{
$weapon = new Weapon();
$form = $this->createForm('ArmyDataBundle\Form\WeaponType' , $weapon);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$units = $weapon->getUnits();
$armies = $weapon->getArmies();
$em = $this->getDoctrine()->getManager();
foreach ($armies as $army){
$army->addWeapons($weapon);
$em->persist($army);
$em->flush($army);
}
foreach ($units as $unit){
$unit->addWeapons($weapon);
$em->persist($unit);
$em->flush($unit);
}
$em->persist($weapon);
$em->flush($weapon);
return $this->redirectToRoute('wp_index');
}