我有3个实体,军队,武器和单位。对于军队和单位,我没有任何问题,但是当我坚持时 武器 (它是军队和单位的反面)学说将所有内容保存在数据库中,除了数组集合中的数据。
我已经在控制器中完成了转储($weapon),并且使用我分配的所有元素正确创建了对象。
实体
武器(反面和数组集合不保存的那边)
<?php
namespace ArmyDataBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* Weapon
*
* @ORMTable(name="weapon")
*@ORMEntity(repositoryClass="ArmyDataBundleRepositoryWeaponRepository")
*/
class Weapon
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @var int
*
* @ORMColumn(name="distance", type="integer")
*/
private $distance;
/**
* @var int
*
* @ORMColumn(name="f", type="integer")
*/
private $f;
/**
* @var int
*
* @ORMColumn(name="fp", type="integer")
*/
private $fp;
/**
* @var string
*
* @ORMColumn(name="type", type="string", length=255)
*/
private $type;
/**
* @var int
*
* @ORMColumn(name="shoots", type="integer")
*/
private $shoots;
/**
* Many Weapons have many Armies
* @ORMManyToMany(targetEntity="ArmyDataBundleEntityArmy", mappedBy="weapons")
*/
private $armies;
/**
* Many Weapons have many Units
* @ORMManyToMany(targetEntity="Unit", mappedBy="weapons")
*/
private $units;
public function __construct()
{
$this->armies = new DoctrineCommonCollectionsArrayCollection();
$this->units = new DoctrineCommonCollectionsArrayCollection();
}
/**
* 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( ArmyDataBundleEntityArmy $army)
{
if (!$this->armies->contains($army)){
$army->addWeapons($this);
$this->armies->add($army);
}
return $this;
}
/**
* Remove army
*
* @param $army
*/
public function removeArmies( ArmyDataBundleEntityArmy $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( ArmyDataBundleEntityUnit $unit)
{
if (!$this->units->contains($unit)){
$this->units = $unit;
$unit->addWeapons($this);
}
return $this;
}
/**
* Remove unit
*
* @param $unit
*/
public function removeUnits( ArmyDataBundleEntityUnit $unit)
{
$this->units->removeElement($unit);
}
/**
*
*/
public function clearUnits(){
$this->units->clear();
}
public function __toString()
{
return (String)$this->name;
}
}
军队
<?php
namespace ArmyDataBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentValidatorConstraints as Assert;
/**
* Army
*
* @ORMTable(name="army")
*
@ORMEntity(repositoryClass="ArmyDataBundleRepositoryArmyRepository")
*/
class Army
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* One army has Many Units
* @ORMOneToMany(targetEntity="ArmyDataBundleEntityUnit", mappedBy="army")
*/
private $units;
/**
* Many armies has Many Weapons
* @ORMManyToMany(targetEntity="Weapon",cascade={"persist"}, inversedBy="armies")
*/
private $weapons;
public function __construct()
{
$this->units = new DoctrineCommonCollectionsArrayCollection();
$this->weapons = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Army
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
public function __toString()
{
return (String)$this->name;
}
/**
* @return mixed
*/
public function getUnits()
{
return $this->units;
}
public function addUnit( ArmyDataBundleEntityUnit $unit)
{
$unit->addWeapon($this);
$this->units->add($unit);
return $this;
}
/**
* Remove unit
*
* @param $unit
*/
public function removeUnit( ArmyDataBundleEntityUnit $unit)
{
$this->units->removeElement($unit);
}
/**
*
*/
public function clearUnits(){
$this->units->clear();
}
/**
* @return mixed
*/
public function getWeapons()
{
return $this->weapons;
}
public function addWeapons( ArmyDataBundleEntityWeapon $weapon)
{
$weapon->addArmies($this);
$this->weapons->add($weapon);
return $this;
}
/**
* Remove unit
*
* @param $unit
*/
public function removeWeapons( ArmyDataBundleEntityWeapon $weapon)
{
$this->weapons->removeElement($weapon);
}
/**
*
*/
public function clearWeapons(){
$this->weapons->clear();
}
}
单位
<?php
namespace ArmyDataBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use DoctrineCommonCollectionsArrayCollection;
/**
* Unit
*
* @ORMTable(name="unit")
*
@ORMEntity(repositoryClass="ArmyDataBundleRepositoryUnitRepository")
*/
class Unit
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*
*/
private $name;
/**
* @var int
*
* @ORMColumn(name="Ha", type="integer")
*/
private $ha;
/**
* @var int
*
* @ORMColumn(name="Hp", type="integer")
*/
private $hp;
/**
* @var int
*
* @ORMColumn(name="F", type="integer")
*/
private $f;
/**
* @var int
*
* @ORMColumn(name="R", type="integer")
*/
private $r;
/**
* @var int
*
* @ORMColumn(name="H", type="integer")
*/
private $h;
/**
* @var int
*
* @ORMColumn(name="I", type="integer")
*/
private $i;
/**
* @var int
*
* @ORMColumn(name="A", type="integer")
*/
private $a;
/**
* @var int
*
* @ORMColumn(name="L", type="integer")
*/
private $l;
/**
* @var int
*
* @ORMColumn(name="S", type="integer")
*/
private $s;
/**
* Many Units have one Army
* @ORMManyToOne(targetEntity="ArmyDataBundleEntityArmy", inversedBy="units")
* @ORMJoinColumn(name="army_id", referencedColumnName="id")
*/
private $army;
/**
* many units has Many Weapons
* @ORMManyToMany(targetEntity="Weapon", cascade={"persist"}, inversedBy="units")
*/
private $weapons;
public function __construct()
{
$this->weapons = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Unit
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set ha
*
* @param integer $ha
*
* @return Unit
*/
public function setHa($ha)
{
$this->ha = $ha;
return $this;
}
/**
* Get ha
*
* @return int
*/
public function getHa()
{
return $this->ha;
}
/**
* Set hp
*
* @param integer $hp
*
* @return Unit
*/
public function setHp($hp)
{
$this->hp = $hp;
return $this;
}
/**
* Get hp
*
* @return int
*/
public function getHp()
{
return $this->hp;
}
/**
* Set f
*
* @param integer $f
*
* @return Unit
*/
public function setF($f)
{
$this->f = $f;
return $this;
}
/**
* Get f
*
* @return int
*/
public function getF()
{
return $this->f;
}
/**
* Set r
*
* @param integer $r
*
* @return Unit
*/
public function setR($r)
{
$this->r = $r;
return $this;
}
/**
* Get r
*
* @return int
*/
public function getR()
{
return $this->r;
}
/**
* Set h
*
* @param integer $h
*
* @return Unit
*/
public function setH($h)
{
$this->h = $h;
return $this;
}
/**
* Get h
*
* @return int
*/
public function getH()
{
return $this->h;
}
/**
* Set i
*
* @param integer $i
*
* @return Unit
*/
public function setI($i)
{
$this->i = $i;
return $this;
}
/**
* Get i
*
* @return int
*/
public function getI()
{
return $this->i;
}
/**
* Set a
*
* @param integer $a
*
* @return Unit
*/
public function setA($a)
{
$this->a = $a;
return $this;
}
/**
* Get a
*
* @return int
*/
public function getA()
{
return $this->a;
}
/**
* Set l
*
* @param integer $l
*
* @return Unit
*/
public function setL($l)
{
$this->l = $l;
return $this;
}
/**
* Get l
*
* @return int
*/
public function getL()
{
return $this->l;
}
/**
* Set s
*
* @param integer $s
*
* @return Unit
*/
public function setS($s)
{
$this->s = $s;
return $this;
}
/**
* Get s
*
* @return int
*/
public function getS()
{
return $this->s;
}
/**
* @return mixed
*/
public function getArmy()
{
return $this->army;
}
/**
* @param mixed $army
*/
public function setArmy($army)
{
$this->army = $army;
}
/**
* @return mixed
*/
public function getWeapons()
{
return $this->weapons;
}
/**
* @param mixed $weapons
*/
public function setWeapons($weapons)
{
$this->weapons = $weapons;
}
public function addWeapons( ArmyDataBundleEntityWeapon $weapon)
{
if (!$this->weapons->contains($weapon)){
$weapon->addUnits($this);
$this->weapons->add($weapon);
}
return $this;
}
/**
* Remove weapon
*
* @param $weapon
*/
public function removeWeapons( ArmyDataBundleEntityWeapon $weapon)
{
$this->weapons->removeElement($weapon);
}
/**
*
*/
public function clearWeapons(){
$this->weapons->clear();
}
public function __toString()
{
return (String)$this->name;
}
}
这是控制器中的方法
public function addAction(Request $request)
{
$weapon = new Weapon();
$form = $this->createForm('ArmyDataBundleFormWeaponType' , $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 ArmyDataBundleForm;
use ArmyDataBundleEntityArmy;
use ArmyDataBundleEntityUnit;
use ArmyDataBundleEntityWeapon;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeDateType;
use SymfonyComponentFormExtensionCoreTypeEmailType;
use SymfonyComponentFormExtensionCoreTypeHiddenType;
use SymfonyComponentFormExtensionCoreTypeIntegerType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
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' => 'ArmyDataBundleEntityWeapon' ,
));
}
public function getBlockPrefix()
{
return 'weapon';
}
}
整个项目正在 https://github.com/erethilclaw/AssaultW40k/tree/development
感谢您的关注。
好的,我解决了,我不知道是否是最好的方法,但我修改了控制器,现在可以工作了。
public function addAction(Request $request)
{
$weapon = new Weapon();
$form = $this->createForm('ArmyDataBundleFormWeaponType' , $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');
}