0

我试图坚持收集表格......奇怪的是代码在 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 和 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;
    }
4

1 回答 1

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

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

于 2013-06-12T07:49:38.367 回答