duanlanzhi5509 2015-06-11 22:25
浏览 39
已采纳

Doctrine ObjectSelect上引用实体的数据类型错误

I have a form which uses Doctrine's ObjectSelect to make a dropdown

$this->add(array(
        'name' => 'category',
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'options' => array(
            'label' => 'Category',
            'object_manager' => $em,
            'target_class' => 'Blog\Entity\Category',
            'property' => 'name'
        ),
        'attributes' => array(
            'required' => true
        )
 ));

the issue I have, is that this should reference the id of another entity, howeve it keeps throwing "Expected value of type "Blog\Entity\Category" for association field "Blog\Entity\Post#$category", got "string" instead."

here´s the var_dump from the form, the important part

object(Zend\InputFilter\InputFilter)[337]
  protected 'factory' => null
  protected 'data' => 
    array (size=5)
      'id' => string '' (length=0)
      'title' => string 'asdasd' (length=6)
      'content' => string '<p>asdasd</p>' (length=13)
      'category' => string '3' (length=1)
      'submit' => string 'Add' (length=3)

and my addAction

public function addAction()
{   
    $form = new PostForm($this->getEntityManager());
    $form->setHydrator(new DoctrineEntity($this->getEntityManager(),'Blog\Entity\Post'));

    $form->get('submit')->setValue('Add');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $post = new Post();
        $form->setInputFilter($post->getInputFilter());
        $form->setData($request->getPost());

        $form->isValid();
        //debug
        var_dump($post);

        $post->exchangeArray($form->getData());
        $em = $this->getEntityManager();
        $em->persist($post);
        $em->flush();
        $this->flashMessenger()->addSuccessMessage('Post Saved');
        return $this->redirect()->toRoute('post');
    }
     return new ViewModel(array(
        'post' => $post,
        'form' => $form
    ));
}

How would I solve this, I don't get how it doesn't complain about the ids but for this it does.

UPDATE ** category entity ORM anotations

/**
 *
 * @ORM\ManyToOne(targetEntity="Category")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
private $category;
  • 写回答

1条回答 默认 最新

  • douzhi9939 2015-06-11 22:30
    关注

    What are your hydrator settings on this fieldset?

    For example;

    $this->setHydrator(new DoctrineHydrator($em, 'Blog\Entity\Category'))
                 ->setObject(
                     new Category()
                 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大