Symfony 3 预计 "Doctrine\Common\Collections\ArrayCollection" 给出 ... 使用 OneToMany 时
Symfony 3 expected "Doctrine\Common\Collections\ArrayCollection" given ... when using OneToMany
我对这个错误有疑问:
Expected argument of type "AppBundle\Entity\BusVehiclesAmenities", "Doctrine\Common\Collections\ArrayCollection" given
编辑 3
经过一些评论后,我应用了建议,现在错误消失了(从公交车便利设施实体中删除了 ArrayCollection,从控制器中删除了设置公交车便利设施实体,并删除了所有映射实体上的 ORM 连接列)
现在我有这个错误:
The association AppBundle\Entity\BusVehiclesAmenities#bus refers to the inverse side field AppBundle\Entity\BusVehicles#busVehiclesAmenities which does not exist.
映射实体
公交车辆:
/* @ORM\OneToMany(
* targetEntity="BusVehiclesAmenities",
* mappedBy="bus",
* fetch="EXTRA_LAZY",
* orphanRemoval=true,
* cascade={"persist"}
* )
*
*/
private $busVehiclesAmenities;
公交车辆设施实体:
private $bus;
/**
*
* @ORM\ManyToOne(targetEntity="Amenities", inversedBy="amenities")
* @ORM\JoinColumn(name="amenities_id", referencedColumnName="id")
*
*/
我正在使用表单集合来创建一对多关系。问题是当我添加新数据时出现此错误。
有人知道问题出在哪里吗?
代码:
总线实体(父)
/**
* @ORM\OneToMany(targetEntity="BusVehiclesAmenities", mappedBy="bus", cascade={"persist", "remove"}, fetch="EAGER", orphanRemoval=true)
* @ORM\JoinColumn(name="bus_id", referencedColumnName="id", onDelete="CASCADE")
*
*/
private $busVehiclesAmenities;
public function __construct()
{
$this->busVehiclesAmenities = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add busVehiclesAmenities
*
* @param \AppBundle\Entity\BusVehiclesAmenities busVehiclesAmenities
* @return BusVehicles
*/
public function addBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
if ($this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
return;
}
$this->busVehiclesAmenities->add($busVehiclesAmenities);
$busVehiclesAmenities->addBus($this);
return $this;
}
/**
* Set busVehiclesAmenities
*
* @param \AppBundle\Entity\BusVehiclesAmenities busVehiclesAmenities
* @return BusVehicles
*/
public function setBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
$this->busVehiclesAmenities = $busVehiclesAmenities;
return $this;
}
/**
* Remove busVehiclesAmenities
*
* @param \AppBundle\Entity\BusVehiclesAmenities $busVehiclesAmenities
* @return BusVehicles
*/
public function removeBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
if (!$this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
return;
}
$this->busVehiclesAmenities->removeElement($busVehiclesAmenities);
$busVehiclesAmenities->removeBus($this);
return $this;
}
便利设施实体(儿童)
/**
*
* @ORM\ManyToOne(targetEntity="BusVehicles", inversedBy="busVehiclesAmenities")
* @ORM\JoinColumn(name="bus_id", referencedColumnName="id")
*
*/
private $bus;
public function __construct()
{
$this->bus = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param BusVehicles $busVehicles
*/
public function addBus(BusVehicles $busVehicles)
{
if ($this->bus->contains($busVehicles)) {
return;
}
$this->bus->add($busVehicles);
$busVehicles->addBusVehiclesAmenities($this);
}
/**
* @param BusVehicles $busVehicles
*/
public function removeBus(BusVehicles $busVehicles)
{
if (!$this->bus->contains($busVehicles)) {
return;
}
$this->bus->removeElement($busVehicles);
$busVehicles->removeBusVehiclesAmenities($this);
}
编辑 1:
我从 BusVehiclesAmenitites 实体中删除了 ArrayCollection,但我在提交表单时仍然遇到同样的错误。
编辑 2:
已添加表格
BusVehicles 表单类型
<?php
namespace AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class BusVehiclesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('licencePlate')
->add('chassisNumber')
->add('manufacturer')
->add('busVehiclesAmenities', CollectionType::class, array(
'entry_type' => BusVehiclesAmenitiesType::class,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\BusVehicles',
'cascade_validation' => true,
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'adminbundle_busvehicles';
}
}
BusVehiclesAmenities 表格类型
<?php
namespace AdminBundle\Form\Type;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\BusVehiclesAmenities;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BusVehiclesAmenitiesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price', NumberType::class, array(
))
->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';
}
}
控制器。
/**
* @Template
*/
public function newAction(Request $request)
{
$bus = new BusVehicles();
$busAmenities = new BusVehiclesAmenities();
// $this->getDoctrine()->getManager()->persist($busAmenities);
$bus->setBusVehiclesAmenities($busAmenities);
$form = $this->createForm(BusVehiclesType::class, $bus);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
/*$form->get('busVehiclesAmenities')->getData()->map(
function ($amenities) use ($em, $bus) {
$bus->setBusVehiclesAmenities($amenities);
$em->persist($amenities);
}
);*/
$em->persist($bus);
$em->flush();
// Adding flash message to our user
$this->addFlash('success', 'admin.bus.created');
// return $this->redirectToRoute('bus_add');
}
return [
'form' => $form->createView(),
];
}
BusVehiclesAmenities->bus
是 object,不是集合。 $this->bus = new ArrayCollection();
行没有理由存在于您的 child 实体中。
您想在 object(总线)中实现 ManyToOne,也就是 ArrayCollection
,它是单个 object.
编辑
在您的控制器中执行以下操作
$bus->setBusVehiclesAmenities($busAmenities);
这不是必需的。表单将自行处理集合。 魔法
您的错误可能来自此调用,但这是因为您试图说 arrayCollection = object
这是不可能的。
public function setBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
// ArrayCollection = Object
$this->busVehiclesAmenities = $busVehiclesAmenities;
return $this;
}
您可以尝试执行 setBusVehiclesAmenities(ArrayCollection $busVehiclesAmenities)
或 Collection
,我不知道,或者删除 setBusVehiclesAmenities
方法。 (我个人会删除该方法)
另外你应该检查你的 getter/setter 在你的 child 中是否有 bus
,这次你的变量是一个单一的 object,只做 getBus()
和setBus($bus)
,不是添加和删除。
希望对您有所帮助
从便利设施实体(儿童)中删除这部分代码:
public function __construct()
{
$this->bus = new ArrayCollection();
}
如果表单定义正确,应该可以解决您的问题。
说明 - 此实体中的总线是 OneToMany 关系的拥有方。永远是一个,不可能是合集。
我 运行 遇到了与您类似的问题。我为解决此问题所做的是删除 JoinColumn。它实际上是多余的。恕我直言,这是关于 Symfony 的 "magic" 事情之一。只需确保您的 OneToMany 注释正确指向您的 ManyToOne。这是一个例子——这是在我的 User.php 实体中:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\UserStats", mappedBy="user")
*/
private $userStats;
这是在被加入的实体中(UserStats.php):
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User",
* inversedBy="userStats")
*
*/
private $user;
如您所见,没有 JoinColumn,因此一切都运行良好。我希望这可以帮助你。 :)
确保你使用我上面的正确路径,即
* (targetEntity="AppBundle\Entity\User", inversedBy="userStats")
我对这个错误有疑问:
Expected argument of type "AppBundle\Entity\BusVehiclesAmenities", "Doctrine\Common\Collections\ArrayCollection" given
编辑 3
经过一些评论后,我应用了建议,现在错误消失了(从公交车便利设施实体中删除了 ArrayCollection,从控制器中删除了设置公交车便利设施实体,并删除了所有映射实体上的 ORM 连接列)
现在我有这个错误:
The association AppBundle\Entity\BusVehiclesAmenities#bus refers to the inverse side field AppBundle\Entity\BusVehicles#busVehiclesAmenities which does not exist.
映射实体
公交车辆:
/* @ORM\OneToMany(
* targetEntity="BusVehiclesAmenities",
* mappedBy="bus",
* fetch="EXTRA_LAZY",
* orphanRemoval=true,
* cascade={"persist"}
* )
*
*/
private $busVehiclesAmenities;
公交车辆设施实体:
private $bus;
/**
*
* @ORM\ManyToOne(targetEntity="Amenities", inversedBy="amenities")
* @ORM\JoinColumn(name="amenities_id", referencedColumnName="id")
*
*/
我正在使用表单集合来创建一对多关系。问题是当我添加新数据时出现此错误。
有人知道问题出在哪里吗?
代码: 总线实体(父)
/**
* @ORM\OneToMany(targetEntity="BusVehiclesAmenities", mappedBy="bus", cascade={"persist", "remove"}, fetch="EAGER", orphanRemoval=true)
* @ORM\JoinColumn(name="bus_id", referencedColumnName="id", onDelete="CASCADE")
*
*/
private $busVehiclesAmenities;
public function __construct()
{
$this->busVehiclesAmenities = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add busVehiclesAmenities
*
* @param \AppBundle\Entity\BusVehiclesAmenities busVehiclesAmenities
* @return BusVehicles
*/
public function addBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
if ($this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
return;
}
$this->busVehiclesAmenities->add($busVehiclesAmenities);
$busVehiclesAmenities->addBus($this);
return $this;
}
/**
* Set busVehiclesAmenities
*
* @param \AppBundle\Entity\BusVehiclesAmenities busVehiclesAmenities
* @return BusVehicles
*/
public function setBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
$this->busVehiclesAmenities = $busVehiclesAmenities;
return $this;
}
/**
* Remove busVehiclesAmenities
*
* @param \AppBundle\Entity\BusVehiclesAmenities $busVehiclesAmenities
* @return BusVehicles
*/
public function removeBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
if (!$this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
return;
}
$this->busVehiclesAmenities->removeElement($busVehiclesAmenities);
$busVehiclesAmenities->removeBus($this);
return $this;
}
便利设施实体(儿童)
/**
*
* @ORM\ManyToOne(targetEntity="BusVehicles", inversedBy="busVehiclesAmenities")
* @ORM\JoinColumn(name="bus_id", referencedColumnName="id")
*
*/
private $bus;
public function __construct()
{
$this->bus = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param BusVehicles $busVehicles
*/
public function addBus(BusVehicles $busVehicles)
{
if ($this->bus->contains($busVehicles)) {
return;
}
$this->bus->add($busVehicles);
$busVehicles->addBusVehiclesAmenities($this);
}
/**
* @param BusVehicles $busVehicles
*/
public function removeBus(BusVehicles $busVehicles)
{
if (!$this->bus->contains($busVehicles)) {
return;
}
$this->bus->removeElement($busVehicles);
$busVehicles->removeBusVehiclesAmenities($this);
}
编辑 1:
我从 BusVehiclesAmenitites 实体中删除了 ArrayCollection,但我在提交表单时仍然遇到同样的错误。
编辑 2:
已添加表格
BusVehicles 表单类型
<?php
namespace AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class BusVehiclesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('licencePlate')
->add('chassisNumber')
->add('manufacturer')
->add('busVehiclesAmenities', CollectionType::class, array(
'entry_type' => BusVehiclesAmenitiesType::class,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\BusVehicles',
'cascade_validation' => true,
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'adminbundle_busvehicles';
}
}
BusVehiclesAmenities 表格类型
<?php
namespace AdminBundle\Form\Type;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\BusVehiclesAmenities;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BusVehiclesAmenitiesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price', NumberType::class, array(
))
->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';
}
}
控制器。
/**
* @Template
*/
public function newAction(Request $request)
{
$bus = new BusVehicles();
$busAmenities = new BusVehiclesAmenities();
// $this->getDoctrine()->getManager()->persist($busAmenities);
$bus->setBusVehiclesAmenities($busAmenities);
$form = $this->createForm(BusVehiclesType::class, $bus);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
/*$form->get('busVehiclesAmenities')->getData()->map(
function ($amenities) use ($em, $bus) {
$bus->setBusVehiclesAmenities($amenities);
$em->persist($amenities);
}
);*/
$em->persist($bus);
$em->flush();
// Adding flash message to our user
$this->addFlash('success', 'admin.bus.created');
// return $this->redirectToRoute('bus_add');
}
return [
'form' => $form->createView(),
];
}
BusVehiclesAmenities->bus
是 object,不是集合。 $this->bus = new ArrayCollection();
行没有理由存在于您的 child 实体中。
您想在 object(总线)中实现 ManyToOne,也就是 ArrayCollection
,它是单个 object.
编辑
在您的控制器中执行以下操作
$bus->setBusVehiclesAmenities($busAmenities);
这不是必需的。表单将自行处理集合。 魔法
您的错误可能来自此调用,但这是因为您试图说 arrayCollection = object
这是不可能的。
public function setBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
// ArrayCollection = Object
$this->busVehiclesAmenities = $busVehiclesAmenities;
return $this;
}
您可以尝试执行 setBusVehiclesAmenities(ArrayCollection $busVehiclesAmenities)
或 Collection
,我不知道,或者删除 setBusVehiclesAmenities
方法。 (我个人会删除该方法)
另外你应该检查你的 getter/setter 在你的 child 中是否有 bus
,这次你的变量是一个单一的 object,只做 getBus()
和setBus($bus)
,不是添加和删除。
希望对您有所帮助
从便利设施实体(儿童)中删除这部分代码:
public function __construct()
{
$this->bus = new ArrayCollection();
}
如果表单定义正确,应该可以解决您的问题。
说明 - 此实体中的总线是 OneToMany 关系的拥有方。永远是一个,不可能是合集。
我 运行 遇到了与您类似的问题。我为解决此问题所做的是删除 JoinColumn。它实际上是多余的。恕我直言,这是关于 Symfony 的 "magic" 事情之一。只需确保您的 OneToMany 注释正确指向您的 ManyToOne。这是一个例子——这是在我的 User.php 实体中:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\UserStats", mappedBy="user")
*/
private $userStats;
这是在被加入的实体中(UserStats.php):
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User",
* inversedBy="userStats")
*
*/
private $user;
如您所见,没有 JoinColumn,因此一切都运行良好。我希望这可以帮助你。 :)
确保你使用我上面的正确路径,即
* (targetEntity="AppBundle\Entity\User", inversedBy="userStats")