Wrong datatype for referenced entity on Doctrine ObjectSelect

744 Views Asked by At

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

There are 1 best solutions below

5
On BEST ANSWER

What are your hydrator settings on this fieldset?

For example;

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