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

时间:2015-06-11 22:25:15

标签: php zend-framework orm doctrine-orm

我有一个使用Doctrine的ObjectSelect制作下拉列表的表单

$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
        )
 ));

我遇到的问题是,这应该引用另一个实体的id,不管它一直在抛出 “关联字段”Blog \ Entity \ Post#$ category“类型”Blog \ Entity \ Category“的预期值,取而代之的是”字符串“。”

这是表格中的var_dump,重要部分

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)

和我的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
    ));
}

我如何解决这个问题,我不知道它是如何不抱怨ids的,但为此它确实如此。

更新**类别实体ORM anotations

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

1 个答案:

答案 0 :(得分:0)

此字段集上的水分器设置是什么?

例如;

$this->setHydrator(new DoctrineHydrator($em, 'Blog\Entity\Category'))
             ->setObject(
                 new Category()
             );