Symfony2,Doctrine2找到关于sth#category的Doctrine \ Common \ Collections \ ArrayCollection类型的实体,但是期待......

时间:2013-09-29 14:30:07

标签: php symfony doctrine-orm

现在我在表单中提交帖子数据时遇到问题(我的表单如下:

Task: <input text>
Category: <multiple select category>
DueDate: <date>
<submit>

) 提交表单后,我会收到此错误:

Found entity of type Doctrine\Common\Collections\ArrayCollection on association Acme\TaskBundle\Entity\Task#category, but expecting Acme\TaskBundle\Entity\Category

我的消息来源:

任务对象 Task.php

<?php

namespace Acme\TaskBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="tasks") 
 */
class Task
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;              

    /**
     * @ORM\Column(type="string", length=200)    
     * @Assert\NotBlank(
     *      message = "Task cannot be empty"      
     * )    
     * @Assert\Length(
     *      min = "3",
     *      minMessage = "Task is too short"         
     * )     
     */     
    protected $task;

    /**
     * @ORM\Column(type="datetime")    
     * @Assert\NotBlank()
     * @Assert\Type("\DateTime")
     */
    protected $dueDate;

    /**
     * @Assert\True(message = "You have to agree")    
     */         
    protected $accepted;

    /**
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="tasks")                       
     */
    protected $category;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->category = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set task
     *
     * @param string $task
     * @return Task
     */
    public function setTask($task)
    {
        $this->task = $task;

        return $this;
    }

    /**
     * Get task
     *
     * @return string 
     */
    public function getTask()
    {
        return $this->task;
    }

    /**
     * Set dueDate
     *
     * @param \DateTime $dueDate
     * @return Task
     */
    public function setDueDate($dueDate)
    {
        $this->dueDate = $dueDate;

        return $this;
    }

    /**
     * Get dueDate
     *
     * @return \DateTime 
     */
    public function getDueDate()
    {
        return $this->dueDate;
    }

    /**
     * Add category
     *
     * @param \Acme\TaskBundle\Entity\Category $category
     * @return Task
     */
    public function addCategory(\Acme\TaskBundle\Entity\Category $category)
    {
        $this->category[] = $category;

        return $this;
    }

    /**
     * Remove category
     *
     * @param \Acme\TaskBundle\Entity\Category $category
     */
    public function removeCategory(\Acme\TaskBundle\Entity\Category $category)
    {
        $this->category->removeElement($category);
    }

    /**
     * Get category
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCategory()
    {
        return $this->category;
    }
}

类别对象 Category.php

<?php

namespace Acme\TaskBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="categories") 
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")             
     */
    protected $id; 

    /**
     * @ORM\Column(type="string", length=200, unique=true)   
     * @Assert\NotNull(message="Choose a category", groups = {"adding"})                   
     */         
    protected $name;  

    /**
     * @ORM\ManyToMany(targetEntity="Task", mappedBy="category")
     */
    private $tasks;            


    public function __toString()
    {
        return strval($this->name);
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->tasks = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Add tasks
     *
     * @param \Acme\TaskBundle\Entity\Task $tasks
     * @return Category
     */
    public function addTask(\Acme\TaskBundle\Entity\Task $tasks)
    {
        $this->tasks[] = $tasks;

        return $this;
    }

    /**
     * Remove tasks
     *
     * @param \Acme\TaskBundle\Entity\Task $tasks
     */
    public function removeTask(\Acme\TaskBundle\Entity\Task $tasks)
    {
        $this->tasks->removeElement($tasks);
    }

    /**
     * Get tasks
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getTasks()
    {
        return $this->tasks;
    }
}

任务类型 TaskType.php

<?php

namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Acme\TaskBundle\Form\Type\Category;

class TaskType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TaskBundle\Entity\Task',
            'cascade_validation' => true,
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('task', 'text', array('label' => 'Task'))
                ->add('dueDate', 'date', array('label' => 'Due Date'))
                ->add('category', new CategoryType(), array('validation_groups' => array('adding')))
                //->add('accepted', 'checkbox')
                ->add('save', 'submit', array('label' => 'Submit'));
    }

    public function getName()
    {
        return 'task';
    }
}

CategoryType CategoryType.php

<?php

namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CategoryType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => null,
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'entity', array(
                      'class' => 'AcmeTaskBundle:Category',
                      'query_builder' => function($repository) { return $repository->createQueryBuilder('c')->orderBy('c.id', 'ASC'); },
                      'property' => 'name',
                      'multiple' => true,
                      'label' => 'Categories',
                      ));
    }

    public function getName()
    {
        return 'category';
    }
}

和我的控制器 DefaultController.php

public function newAction(Request $request)
{
    $task = new Task();
    $task->setTask('Write name here...');
    $task->setDueDate(new \DateTime('tomorrow'));

    $form = $this->createForm('task', $task);                   
    $form->handleRequest($request);

    if($form->isValid())
    {
        $this->get('session')->getFlashBag()->add(
            'success',
            'Task was successfuly created'
        );
        $em = $this->getDoctrine()->getManager();
        /*
        $category = $this->getDoctrine()->getManager()->getRepository('AcmeTaskBundle:Category')->findOneByName($form->get('category')->getData());
        $task->setCategory($category);
        */
        $em->persist($task);
        try {
            $em->flush();
        } catch (\PDOException $e) {
            // sth
        }

        //$nextAction = $form->get('saveAndAdd')->isClicked() ? 'task_new' : 'task_success';

        //return $this->redirect($this->generateUrl($nextAction));
    } 

    return $this->render('AcmeTaskBundle:Default:new.html.twig', array('form' => $form->createView()));
}

所以,我在谷歌看了这个问题,但是有各种各样的问题。有什么想法吗?

更新 完整的错误消息:

[2013-09-30 14:43:55] request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\ORMException: "Found entity of type Doctrine\Common\Collections\ArrayCollection on association Acme\TaskBundle\Entity\Task#category, but expecting Acme\TaskBundle\Entity\Category" at /var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 762 {"exception":"[object] (Doctrine\\ORM\\ORMException: Found entity of type Doctrine\\Common\\Collections\\ArrayCollection on association Acme\\TaskBundle\\Entity\\Task#category, but expecting Acme\\TaskBundle\\Entity\\Category at /var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:762)"} []

更新2

我的TWIG模板 new.html.twig

<html>
<head>
<title>Task create</title>
</head>
<body>
{% for flashMessage in app.session.flashbag.get('success') %}
<div style="display: block; padding: 15px; border: 1px solid green; margin: 15px; width: 450px;">
{{ flashMessage }}
</div>
{% endfor %}

{{ form_start(form, {'action': path ('task_new'), 'method': 'POST', 'attr': {'novalidate': 'novalidate' }}) }}
  {{ form_errors(form) }}

  <div>
    {{ form_label(form.task) }}:<br>  
    {{ form_widget(form.task) }} {{ form_errors(form.task) }}<br>
  </div>
  <div>
    {{ form_label(form.category) }}:<br>
    {{ form_widget(form.category) }} {{ form_errors(form.category) }}   
  </div>
  <div>
    {{ form_label(form.dueDate) }}:<br>
    {{ form_widget(form.dueDate) }} {{ form_errors(form.dueDate) }}<br>
  </div>

{{ form_end(form) }}
</body>
</html>

5 个答案:

答案 0 :(得分:5)

您可以通过更改实体中的setter来改进控制器代码:

Task

public function addCategory(Category $category)
{
    if (!$this->categories->contains($category)) {
        $this->categories->add($category);
        $category->addTask($this); // Fix this
    }
}

Category

public function addTask(Task $task)
{
    if (!this->tasks->contains($task)) {
        $this->tasks->add($task);
        $task->addCategory($this);
    }
}

这会使ArrayCollection中的元素保持唯一,这样您就不必在代码中进行检查,也会自动设置反面。

答案 1 :(得分:2)

所以我找到了解决方案!此代码有效:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('task', 'text', array('label' => 'Task'))
            ->add('dueDate', 'date', array('label' => 'Date', 'format' => 'ddMMMMyyyy'))
            ->add('category', 'entity', array('required' => true, 'multiple' => true, 'class' => 'AcmeTaskBundle:Category', 'query_builder' => function($repository) { return $repository->createQueryBuilder('c')->orderBy('c.id', 'ASC'); },))
            ->add('save', 'submit', array('label' => 'Send'));
}

我的控制器正在以这种形式工作:

if($form->isValid())
{
    $this->get('session')->getFlashBag()->add(
        'success',
        'Task successfuly added'
    );
    $em = $this->getDoctrine()->getManager();
    foreach($form->get('category')->getData() as $cat)
    {
        $task->removeCategory($cat);
        $task->addCategory($cat);
    }
    $em->persist($task);
    try {
        $em->flush();
    } catch (\PDOException $e) {
        // sth
    }
}

我被迫使用$task->removeCategory($cat)因为如果它不在这里,我会得到出版主要索引的错误。

现在我正在尝试使用嵌入式和非嵌入式表单来解决我的问题 - &gt; Symfony2, validation embedded and non-embedded forms with same parameters and different results?

我相信我的问题对于Symfony2有相同问题的初学者会有所帮助。

答案 2 :(得分:1)

您正在定义从任务到类别的manyToMany关系,这意味着您可以为每个任务分配多个类别。如果您尝试在任务中使用多个类别,可以尝试更改此行

->add('category', new CategoryType(), array('validation_groups' => array('adding')))

类似

->add('category', 'collection', array('type' => new CategoryType()))

检查form collectionthis cookbook

上的symfony文档

答案 3 :(得分:0)

我不确定,但是尝试在您的Task实体中添加一个setCategory方法,因为目前您只有add函数,这就是为什么表单组件调用addFunction而不是整个arraycollection的集合。

答案 4 :(得分:0)

我认为你的形式存在问题。

您的任务与分类有许多关系,并存储在字段category中(逻辑上它是一个类别)。但是在表单中你设置了' - &gt; add('category',new CategoryType())'这意味着你添加了一个数组或类似的东西(data_class没有定义),这个类型包含另一个字段。

解决方案是直接在表单中定义字段(不使用独立表单类型)或从实体类型扩展类别表单类型(默认类型为扩展表单类型)。