持久级联时,调用无对象的成员函数

时间:2013-06-12 04:39:31

标签: symfony

我试图坚持收集表格...... 奇怪的是,代码正在使用2.0而不是2.2(我的错误,但我很确定它是)

这是代码的相关部分..

错误

Error: Call to a member function setEventId() on a non-object in C:\wamp\www\new\src\Splurgin\EventsBundle\Entity\SplurginEventEvents.php line 317

在我的表单类型

    ->add('packages' , 'collection', array('type'=>new SplurginEventPackagesType(),
                                            'allow_add'    => true,
                                            'by_reference' => false,
        ))

我的事件实体错误

/**
 * @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

update2:似乎我需要添加,添加和删除功能..但我不确定

更新3:添加了添加和删除方法(就像在文档中一样),但我仍然得到相同的错误但是在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;
    }

1 个答案:

答案 0 :(得分:0)

所以经过很多次(并且我的意思是很多)头发拉动这是问题

包裹类型

中的

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',
    ));
}

这听起来很愚蠢,但我想我需要睡一觉,呵呵