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()
                 );
    
    打赏 评论

相关推荐 更多相似问题