Symfony 持久化级联时对none对象调用成员函数
我正在尝试保存一个收集表单。。。 奇怪的是,代码在2.0上运行,但在2.2上却没有(我可能是错的,但我很确定它是错的) 以下是代码的相关部分 错误 在我的表格类型中Symfony 持久化级联时对none对象调用成员函数,symfony,Symfony,我正在尝试保存一个收集表单。。。 奇怪的是,代码在2.0上运行,但在2.2上却没有(我可能是错的,但我很确定它是错的) 以下是代码的相关部分 错误 在我的表格类型中 ->add('packages' , 'collection', array('type'=>new SplurginEventPackagesType(), 'allow_add' => true,
->add('packages' , 'collection', array('type'=>new SplurginEventPackagesType(),
'allow_add' => true,
'by_reference' => false,
))
错误所在的my events实体
/**
* @ORM\OneToMany(targetEntity="Splurgin\EventsBundle\Entity\SplurginEventPackages", mappedBy="eventId" ,cascade={"persist"})
*/
private $packages;
public function __construct()
{
$this->media = new \Doctrine\Common\Collections\ArrayCollection();
$this->packages = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getPackages()
{
return $this->packages;
}
public function setPackages($packages)
{
$this->packages = $packages ?: new \Doctrine\Common\Collections\ArrayCollection();
foreach ($this->packages as $package) {
$package->setEventId($this); // line of error
}
return $this->packages;
}
我的包实体
/**
* @var integer $eventId
* @ORM\ManyToOne(targetEntity="SplurginEventEvents", inversedBy="packages")
* @ORM\JoinColumn(name="event_id", referencedColumnName="id")
*/
private $eventId;
/**
* Set eventId
*
* @param integer $eventId
*/
public function setEventId(\Splurgin\EventsBundle\Entity\SplurginEventEvents $eventId)
{
$this->eventId = $eventId;
}
/**
* Get eventId
*
* @return integer
*/
public function getEventId()
{
return $this->eventId;
}
包类型
class SplurginEventPackagesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price')
->add('description')
->add('items')
;
}
public function setDefaultOptions(OptionsResolverInterface $options)
{
return array(
'data_class' => 'Splurgin\EventsBundle\Entity\SplurginEventPackages',
);
}
public function getName()
{
return 'packages';
}
}
在我的控制器中,仅仅检查表单是否有效并持久化它并没有什么有趣的事情
更新:我已经更新了js文件
The placeholder was changed from $$name$$ to __name__ in Symfony 2.1
更新2:似乎我需要添加、添加和删除函数。。但是我不确定
更新3:添加了add和remove方法(就像在文档中一样),但仍然在add方法中出现了相同的错误
下面是add方法的代码,唯一更改的代码是setPackages方法
public function getPackages()
{
return $this->packages;
}
public function addPackage( ArrayCollection $package) // if i put the type hinting i get an error that array collection was not passed , i get a normal array instead
{
$package->setEventId($this); // this thoughs an error (because we cant use set event method on an array
$this->packages->add($package);
}
public function removePackage(ArrayCollection $package)
{
//...
}
public function setPackages($packages)
{
$this->packages = $packages ;
return $this->packages;
}
所以在罗得(我指的是很多)拉头发之后,这就是问题所在
在packages类型中
public function setDefaultOptions(OptionsResolverInterface $options)
{
return array(
'data_class' => 'Splurgin\EventsBundle\Entity\SplurginEventPackages',
);
}
应该是
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Splurgin\EventsBundle\Entity\SplurginEventPackages',
));
}
这听起来很愚蠢,但我想我需要睡一觉,嗯请出示您的
splugineventpackagestype
表格。@忘了吧,因为我已经更新了帖子。在317行前后,C:\wamp\www\new\src\splugin\events sbundle\Entity\spluginevents.php中的内容是什么?@Michaelsivolabov谢谢,但我解决了问题issue@codeiz正如我所料,问题出现在splugineventpackagestype
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Splurgin\EventsBundle\Entity\SplurginEventPackages',
));
}