dow57588 2015-06-23 12:48
浏览 69
已采纳

传递的Symfony2参数1必须是一种类型的数组,对象给出错误

A simple problem that has many answers on SO... Yet none of them work on my project... So I get this error:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in C:\wamp\www\Dig\front\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 528 and defined in C:\wamp\www\Digidis\front\vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php line 48

This happens everytime I create a new Email and try to save it in the database. The email is in a relationship with skin..

This is how I try to save it:

/**
     * @Route("/{skin_id}/new", name="cms_email_new")
     * @Method({"GET"})
     * @Template()
     */
    public function newAction($skin_id) {
        $skin = $this->getRepository('ProjectSkinBundle:Skin')->find($skin_id);
        $item = new Email();
        $form = $this->createForm(new EmailType($this->container->getParameter("langs")), $item);

        return array('form' => $form->createView(), 'item' => $item, 'skin' => $skin_id);
    }

    /**
     * @Route("/{skin_id}/save", name="cms_email_save")
     * @Template("ProjectUserBundle:EmailAdmin:new.html.twig")
     * @Method({"POST"})
     */
    public function saveAction(Request $request, $skin_id) {

        $skin = $this->getRepository('ProjectSkinBundle:Skin')->find($skin_id);
        $item = new Email();
        $type = new EmailType($this->container->getParameter("langs"));
        $form = $this->createForm($type, $item);
        $form->handleRequest($request);
        $em = $this->getEntityManager();
        if ($form->isValid()) {

            $this->upload($form, $item);

            $skin->setEmailId($item);
            $item->setSkin($skin);  /// the error is here
            $em->persist($skin);
            $em->persist($item);
            $em->flush();
            return $this->redirect($this->generateUrl('cms_skin_email_edit', array('skin_id' => $skin_id)));
        }

        return array('form' => $form->createView(), 'item' => $item);
    }

So by doing some testing I found out that this line is causing the problem:

$item->setSkin($skin);

Without this line everything works like a charm. However I need this line to work.

So this is the Entity with the setSkin method:

/**
 *
 * @ORM\OneToMany(targetEntity="Project\SkinBundle\Entity\Skin", mappedBy="email_id")
 * @ORM\JoinColumn(name="skin", referencedColumnName="id")
 */
protected $skin;

    /**
     * Set skin
     *
     * @param \Project\SkinBundle\Entity\Skin $skin
     * @return Email
     */
    public function setSkin(\Project\SkinBundle\Entity\Skin $skin = null)
    {
        $this->skin = $skin;

        return $this;
    }

    /**
     * Get skin
     *
     * @return \Project\SkinBundle\Entity\Skin
     */
    public function getSkin()
    {
        return $this->skin;
    }

So what can I do to make his object become an array?

I have this little line but id doesnt help me :

public function __construct()
{
    $this->skin = new ArrayCollection();
}

The form for creating a new email is this:

    public function buildForm(FormBuilderInterface $builder, array $option) {

        $builder->add('title', 'text', array('label' => 'cms.Title'));

}
public function getDefaultOptions(array $options) {
    return array(
        'data_class' => 'Project\UserBundle\Entity\Email',
    );
}
public function getName()
{
    return 'my_email';
}

}

  • 写回答

3条回答 默认 最新

  • dty5753 2015-06-23 13:30
    关注

    The $skin property is a One to Many relationship in your doctrine mapping. Doctrine is expecting an ArrayCollection object or array.

    This is causing your exception:

    /**
     *
     * @ORM\OneToMany(targetEntity="Project\SkinBundle\Entity\Skin", mappedBy="email_id")
     * @ORM\JoinColumn(name="skin", referencedColumnName="id")
     */
    protected $skin;
    

    If you need a one to many relationship you should pass an array instead of a single object because you can have multiple skins. If you want a one to one relationship (a single skin per entity) you should change you doctrine mapping.

    Possible solution 1:

    public function __construct()
    {
        $this->skin = new ArrayCollection();
    }
    
    /**
     * Set skin
     *
     * @param \Project\SkinBundle\Entity\Skin $skin
     * @return Email
     */
    public function setSkin(array $skin)
    {
        $this->skin = $skin;
    
        return $this;
    }
    
    /**
     * Get skin
     *
     * @return \Project\SkinBundle\Entity\Skin[]|ArrayCollection
     */
    public function getSkin()
    {
        return $this->skin;
    }
    

    Possible solution 2 (OneToOne, but this could be a ManyToOne, that's up to you):

    /**
     *
     * @ORM\OneToOne(targetEntity="Project\SkinBundle\Entity\Skin", mappedBy="email_id")
     * @ORM\JoinColumn(name="skin", referencedColumnName="id")
     */
    protected $skin;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?