我之前使用过的确切功能没有任何问题。我现在一直在寻找几个小时,只是无法弄清楚出了什么问题。希望你能帮助我!任何帮助将不胜感激。
所以我有以下实体:
LocationBike:
class LocationBike extends TranslatableEntity
{
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\LocationBikeAccessory", mappedBy="locationBike", cascade={"persist"}, orphanRemoval=true)
*/
protected $accessorys;
public function __construct()
{
$this->accessorys = new ArrayCollection();
}
/**
* Add bike accessory.
*
* @param LocationBikeAccessory $bike
*
* @return LocationBike
*/
public function addAccessory(LocationBikeAccessory $accessory = null)
{
$accessory->setLocationBike($this);
$this->accessorys[] = $accessory;
return $this;
}
/**
* Remove bike accessory.
*
* @param LocationBikeAccessory
*/
public function removeAccessory(LocationBikeAccessory $accessory)
{
$this->accessorys->removeElement($accessory);
}
/**
* Get accessorys.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAccessorys()
{
return $this->accessorys;
}
}
LocationBikeAccessory:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="app_location_bike_accessory")
*/
class LocationBikeAccessory
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\LocationBike", inversedBy="accessorys", cascade={"persist"})
**/
protected $locationBike;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Accessory", cascade={"persist"})
**/
protected $accessory;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $price;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set location bike.
*
* @param LocationBike $bike
*/
public function setLocationBike(LocationBike $bike)
{
$this->locationBike = $bike;
}
/**
* Get location bike.
*
* @return LocationBike $bike
*/
public function getLocationBike()
{
return $this->locationBike;
}
/**
* Set accessory.
*
* @param Accessory $accessory
*/
public function setAccessory(Accessory $accessory)
{
$this->accessory = $accessory;
}
/**
* Get specification.
*
* @return Accessory $accessory
*/
public function getAccessory()
{
return $this->accessory;
}
/**
* Set price.
*
* @param float $price
*
* @return LocationBikeAccessory
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price.
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
}
表单类型如下所示:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* LocationBike form.
*/
class LocationBikeType extends AbstractType
{
/**
* Build form.
*
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
///
->add('accessorys', 'collection', array(
'type' => new LocationBikeAccessoryType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
))
;
}
/**
* Set default options.
*
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\LocationBike',
));
}
/**
* Returns form name.
*
* @return string form name
*/
public function getName()
{
return 'location_bike';
}
}
这一个:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* LocationBikeAccessory form.
*/
class LocationBikeAccessoryType extends AbstractType
{
/**
* Build form.
*
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('accessory', null, array('required' => true))
->add('price', 'sylius_money', array('required' => true))
;
}
/**
* Set default options.
*
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\LocationBikeAccessory',
));
}
/**
* Returns form name.
*
* @return string form name
*/
public function getName()
{
return 'location_bike_accessory';
}
}
当我提交表单时,我收到以下错误:
警告:spl_object_hash()期望参数1为object,给定为null
问题似乎只是当我尝试通过提交表单添加新项目时。如果我在控制器中添加一个项目,它将保存得很好。使用表单更新它们也可以。
使用stacktrace进行编辑:
[1] Symfony\Component\Debug\Exception\ContextErrorException: Warning: spl_object_hash() expects parameter 1 to be object, null given
at n/a
in /var/www/html/bimbim/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 2445
at Symfony\Component\Debug\ErrorHandler->handleError('2', 'spl_object_hash() expects parameter 1 to be object, null given', '/var/www/html/bimbim/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php', '2445', array('entity' => null))
in line
at spl_object_hash(null)
in /var/www/html/bimbim/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 2445
at Doctrine\ORM\UnitOfWork->cancelOrphanRemoval(null)
in /var/www/html/bimbim/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php line 475
at Doctrine\ORM\PersistentCollection->set('0', null)
in /var/www/html/bimbim/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php line 522
at Doctrine\ORM\PersistentCollection->offsetSet('0', null)
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 219
at Symfony\Component\PropertyAccess\PropertyAccessor->readPropertiesUntil(object(PersistentCollection), object(PropertyPath), '1', true)
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 58
at Symfony\Component\PropertyAccess\PropertyAccessor->getValue(object(PersistentCollection), object(PropertyPath))
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 57
at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(PersistentCollection), object(RecursiveIteratorIterator))
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 921
at Symfony\Component\Form\Form->add('0', object(LocationBikeAccessoryType), array('property_path' => '[0]', 'block_name' => 'entry'))
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php line 128
at Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener->preSubmit(object(FormEvent), 'form.pre_bind', object(EventDispatcher))
in line
at call_user_func(array(object(ResizeFormListener), 'preSubmit'), object(FormEvent), 'form.pre_bind', object(EventDispatcher))
in /var/www/html/bimbim/app/cache/dev/classes.php line 1949
at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(array(object(BindRequestListener), 'preBind'), array(object(TrimListener), 'preSubmit'), array(object(CsrfValidationListener), 'preSubmit'), array(object(ResizeFormListener), 'preSubmit')), 'form.pre_bind', object(FormEvent))
in /var/www/html/bimbim/app/cache/dev/classes.php line 1882
at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('form.pre_bind', object(FormEvent))
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php line 43
at Symfony\Component\EventDispatcher\ImmutableEventDispatcher->dispatch('form.pre_bind', object(FormEvent))
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 551
at Symfony\Component\Form\Form->submit(array(array('accessory' => '2', 'price' => '5')), true)
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 572
at Symfony\Component\Form\Form->submit(array('bike' => '6', 'translations' => array('en' => array('description' => '', 'promo' => ''), 'nl' => array('description' => 'fdg', 'promo' => 'fg')), 'price_1_hour' => '', 'price_2_hour' => '5,00', 'price_4_hour' => '', 'price_24_hour' => '', 'price_48_hour' => '', 'price_72_hour' => '', 'price_96_hour' => '', 'price_120_hour' => '', 'price_144_hour' => '', 'price_168_hour' => '', 'available' => '1', 'deposit' => '0,00', 'specifications' => array(array('description' => 'dsf'), array('description' => 'dfg')), 'accessorys' => array(array('accessory' => '2', 'price' => '5')), '_token' => 'G55-UP3byjQlrVFw9FLXsfoGzH4Zn8bixv8gWPTLRg0', 'imageFile' => array('file' => null)), true)
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php line 116
at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest(object(Form), object(Request))
in /var/www/html/bimbim/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 498
at Symfony\Component\Form\Form->handleRequest(object(Request))
in /var/www/html/bimbim/src/AppBundle/Controller/Admin/LocationController.php line 284
at AppBundle\Controller\Admin\LocationController->editBikeAction(object(Location), object(LocationBike), object(Request))
in line
at call_user_func_array(array(object(LocationController), 'editBikeAction'), array(object(Location), object(LocationBike), object(Request)))
in /var/www/html/bimbim/app/bootstrap.php.cache line 3054
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
in /var/www/html/bimbim/app/bootstrap.php.cache line 3016
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
in /var/www/html/bimbim/app/bootstrap.php.cache line 3165
at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
in /var/www/html/bimbim/app/bootstrap.php.cache line 2406
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
in /var/www/html/bimbim/web/app_dev.php line 28
答案 0 :(得分:1)
您使用的是什么版本的symfony?我遇到了这个问题,这是symfony中的一个错误 - https://github.com/symfony/symfony/pull/16090
我使用的是2.7.4,但其他人报告了2.6及更早版本2.7。*版本中的错误。升级到Symfony 2.7.6为我解决了这个问题。
答案 1 :(得分:0)
->add('price', 'sylius_money', array('required' => true))
替换为
->add('price', 'money', array('required' => true))
并且您无法将实体添加为null类型 设置
->add('accessory', 'entity',....) #Optional QueryBuilder function