Symfony 3在一个形式中有多个实体



这两天我一直坚持这个。我正在创建巴士旅行网站。目前,我正处于向数据库添加新总线的阶段。

一辆公共汽车(或所有)有许多便利设施(如空调,WiFi,厨房等),每辆都需要有定制价格。假设我们的巴士有空调。这辆巴士的空调费用为50美元,但其他巴士的空调费用为100美元。为此,我创建了 3 个实体(公交车辆实体、便利设施实体和巴士车辆便利设施实体)。在设施实体内,我将存储巴士的ID,便利设施的ID和该巴士ID的便利设施的价格。

问题是我无法让它工作。在为该字段呈现表单时,我得到了空白的 HTML 元素。我无法添加或删除多个便利设施,也不确定为什么它不起作用。

有人知道问题出在哪里吗?

这是我使用的代码:

设施实体:

<?php
namespace AppBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
* Amenities
*
* @ORMTable(name="amenities", indexes={@ORMIndex(name="administrator_id", columns={"administrator_id"})})
* @ORMEntity
*/
class Amenities
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=100, nullable=true)
*/
private $name;
/**
* @var DateTime
*
* @ORMColumn(name="created_at", type="datetime", nullable=false)
*/
private $createdAt = 'CURRENT_TIMESTAMP';
/**
* @var DateTime
*
* @ORMColumn(name="modified_at", type="datetime", nullable=false)
*/
private $modifiedAt = 'CURRENT_TIMESTAMP';
/**
* @var AppBundleEntityUsers
*
* @ORMManyToOne(targetEntity="Users")
* @ORMJoinColumns({
*   @ORMJoinColumn(name="administrator_id", referencedColumnName="id")
* })
*/
private $administrator;

/**
* @ORMOneToMany(targetEntity="BusVehiclesAmenities", mappedBy="amenities")
*/
private $amenities;

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Amenities
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set createdAt
*
* @param DateTime $createdAt
*
* @return Amenities
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
* Constructor
*/
public function __construct()
{
$this->amenities = new ArrayCollection();
}
/**
* Add amenities
*
* @param AppBundleEntityBusVehiclesAmenities $amenities
* @return Amenities
*/
public function addAmenities(BusVehiclesAmenities $amenities)
{
$this->amenities[] = $amenities;
return $this;
}
/**
* Remove amenities
*
* @param AppBundleEntityBusVehiclesAmenities $amenities
*/
public function removeAmenities(BusVehiclesAmenities $amenities)
{
$this->amenities->removeElement($amenities);
}
/**
* Get amenities_bus_vehicles
*
* @return ArrayCollection
*/
public function getAmenities()
{
return $this->amenities;
}
}

巴士车辆实体:

namespace AppBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
* BusVehicles
*
* @ORMTable(name="bus_vehicles", indexes={@ORMIndex(name="company_id", columns={"company_id"}), @ORMIndex(name="bus_type", columns={"bus_type"}), @ORMIndex(name="fuel_type", columns={"fuel_type"}), @ORMIndex(name="emission_class", columns={"emission_class"})})
* @ORMEntity
*/
class BusVehicles
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="licence_plate", type="string", length=100, nullable=false)
*/
private $licencePlate;

/**
* @var AppBundleEntityCompanies
*
* @ORMManyToOne(targetEntity="Companies")
* @ORMJoinColumns({
*   @ORMJoinColumn(name="company_id", referencedColumnName="id")
* })
*/
private $company;
/**
* @var AppBundleEntityBusTypes
*
* @ORMManyToOne(targetEntity="BusTypes", inversedBy="busType")
* @ORMJoinColumns({
*   @ORMJoinColumn(name="bus_type", referencedColumnName="id")
* })
*/

/**
* @ORMOneToMany(targetEntity="BusVehiclesAmenities", mappedBy="bus")
*/
private $busVehiclesAmenities;
public function __construct()
{
$this->busVehiclesAmenities = new ArrayCollection();
}


/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add busVehiclesAmenities
*
* @param AppBundleEntityBusVehiclesAmenities busVehiclesAmenities
* @return BusVehicles
*/
public function addBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
$this->busVehiclesAmenities[] = $busVehiclesAmenities;
return $this;
}
/**
* Remove busVehiclesAmenities
*
* @param AppBundleEntityBusVehiclesAmenities $busVehiclesAmenities
*/
public function removeBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
$this->busVehiclesAmenities->removeElement($busVehiclesAmenities);
}
/**
* Get busVehiclesAmenities
*
* @return ArrayCollection
*/
public function getBusVehiclesAmenities()
{
return $this->busVehiclesAmenities;
}

/**
* Set licencePlate
*
* @param string $licencePlate
*
* @return BusVehicles
*/
public function setLicencePlate($licencePlate)
{
$this->licencePlate = $licencePlate;
return $this;
}
/**
* Set company
*
* @param AppBundleEntityCompanies $company
*
* @return BusVehicles
*/
public function setCompany(Companies $company = null)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* @return AppBundleEntityCompanies
*/
public function getCompany()
{
return $this->company;
}

}

巴士设施实体:

<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
* BusVehiclesAmenities
*
* @ORMTable(name="bus_vehicles_amenities", indexes={@ORMIndex(name="amenities_id", columns={"amenities_id"}), @ORMIndex(name="bus_id", columns={"bus_id"})})
* @ORMEntity
*/
class BusVehiclesAmenities
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
*
* @ORMManyToOne(targetEntity="BusVehicles", inversedBy="busVehiclesAmenities")
* @ORMJoinColumn(name="bus_id", referencedColumnName="id")
*
*/
private $bus;
/**
*
* @ORMManyToOne(targetEntity="Amenities", inversedBy="amenities")
* @ORMJoinColumn(name="amenities_id", referencedColumnName="id")
*
*/
private $amenities;
/**
* @var float
* @ORMColumn(name="price", type="float", scale=2)
*/
protected $price;

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set Bus
*
* @param AppBundleEntityBusVehicles
*
* @return BusVehiclesAmenities
*/
public function setBus($bus)
{
$this->bus = $bus;
return $this;
}
/**
* Get busId
*
* @return integer
*/
public function getBus()
{
return $this->bus;
}
/**
* Set amenities
*
* @param AppBundleEntityAmenities
*
* @return BusVehiclesAmenities
*/
public function setAmenities($amenities)
{
$this->amenities = $amenities;
return $this;
}
/**
* Get amenities
*
* @return AppBundleEntityAmenities
*/
public function getAmenities()
{
return $this->amenities;
}
/**
* Set price
*
* @param float $price
*
* @return BusVehiclesAmenities
*/
public function sePrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
}

形式:

添加新的总线表单:

<?php
namespace AdminBundleFormType;
use AdminBundleFormTypeBusVehiclesAmenitiesType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;


class BusVehiclesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('licencePlate')
->add('company', EntityType::class, array(
'class' => 'AppBundle:Companies',
'choice_label' => 'name',
->add('busVehiclesAmenities', CollectionType::class, array(
'entry_type'   => BusVehiclesAmenitiesType::class,
'allow_add'    => true,
));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundleEntityBusVehicles'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'adminbundle_busvehicles';
}

}

巴士车辆设施表格

<?php
namespace AdminBundleFormType;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeMoneyType;
use SymfonyComponentFormFormBuilderInterface;
use AppBundleEntityBusVehiclesAmenities;
use SymfonyComponentOptionsResolverOptionsResolver;

class BusVehiclesAmenitiesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price', MoneyType::class, array(
'scale' => 2,
))
->add('amenities', EntityType::class, array(
'class' =>'AppBundle:Amenities',
'choice_label' => 'name',
))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => BusVehiclesAmenities::class
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_busvehiclesamenities';
}

}

我不会将实体类用于表单绑定。 我会创建一个新类(例如 BusVehicle),其中包含表单(特定字段)上需要的所有属性,并将其用作"data_class"。这样,您不仅可以解决问题,还可以将表示层与业务层分离(请参阅多层体系结构)。

我通常将这些类放在目录中Form/Model

在你的例子中,类将是Form/Model/BusVehicle的,它将具有$amenities属性。 便利设施将是一系列Form/Model/Amenity对象。

您可能需要使用嵌入式表单,只是不要将实体类用作"data_object"。成功绑定后,实例化并填充实体以进行持久化或更新。

而且您不需要第三个实体("巴士车辆设施")。

相关内容

  • 没有找到相关文章

最新更新