使用带有属性的ManyToMany表单

时间:2015-02-02 15:06:08

标签: forms symfony attributes many-to-many

我目前有两个实体ManyToMany属性。 我正在创建我的项目创建表单。 我希望我可以选择复选框,我的食谱类别。 我的问题是我的表单中有字段,但数据处理不起作用!我的食谱和我的课程之间的关联不起作用。 对不起,如果我的变量不一致。我更改了变量名称和名称空间。

项目:

<?php

namespace Test\Bundle\ProjectBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;

use Doctrine\ORM\Mapping as ORM;

/**
 * Project
 *
 * @ORM\Table(name="project")
 * @ORM\Entity(repositoryClass="Test\Bundle\ProjectBundle\Entity\ProjectRepository")
 */
class Project
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** 
     * @ORM\OneToMany(targetEntity="Test\Bundle\ProjectBundle\Entity\CategoryProject", mappedBy="projects")
     */
    protected $categoryprojects;
}

&GT;

分类

<?php

namespace Test\Bundle\ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="Test\Bundle\ProjectBundle\Entity\CategoryRepository")
 */
class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** 
     * @ORM\OneToMany(targetEntity="Test\Bundle\ProjectBundle\Entity\CategoryProject", mappedBy="categories")
     */
    protected $categoryprojects;

}
?>

CategoryProject

<?php

namespace Test\Bundle\ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * AppMonde
 *
 * @ORM\Table(name="categoryProject")
 * @ORM\Entity()
 */
class CategoryProject
{

    /** 
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="Test\Bundle\ProjectBundle\Entity\Category", inversedBy="categoryprojects") 
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false) 
     */
    protected $categories;

    /** 
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="Test\Bundle\ProjectBundle\Entity\Project", inversedBy="categoryprojects") 
     * @ORM\JoinColumn(name="project_id", referencedColumnName="id", nullable=false) 
     */
    protected $projects;

        /**
     * @var boolean
     *
     * @ORM\Column(name="booleanos", type="boolean")
     */
    protected $booleanos;


    /**
     * Set a category
     *
     * @param \Test\Bundle\ProjectBundle\Entity\Category $category
     * @return User
     */
    public function addCategory(\Test\Bundle\ProjectBundle\Entity\Category $category)
    {
        $this->categories[] = $category;

        return $this;
    }

    /**
     * Remove a category
     *
     * @param \Test\Bundle\ProjectBundle\Entity\Category $category
     */
    public function removeCategory($category)
    {
        return $this->category->removeElement($category);
    }

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

    /**
     * Set a project
     *
     * @param \Test\Bundle\ProjectBundle\Entity\Project $project
     * @return User
     */
    public function addProject(\Test\Bundle\ProjectBundle\Entity\Project $project)
    {
        $this->projects[] = $project;

        return $this;
    }

    /**
     * Remove a project
     *
     * @param \Test\Bundle\ProjectBundle\Entity\Project $project
     */
    public function removeProject($project)
    {
        return $this->projects->removeElement($project);
    }

    /**
     * Remove all projects
     *
     * @param \Test\Bundle\ProjectBundle\Entity\Project $project
     */
    public function removeAllProjects()
    {
        $this->projects->clear();
    }

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

项目类型

<?php

namespace Test\Bundle\ProjectBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

class ProjectType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', 'text', array(
                'label' => 'Titre de l\'APP'
            ))
            ->add('resume', 'textarea', array(
                'label' => 'Résumé du projet et des actions prévues'
            ))
            ->add('categoryprojects', new CategortyProjectType(), array(
                'label' => '&nbsp;'
            ))
    }

}
?>

CategortyProjectType

<?php

namespace Test\Bundle\ProjectBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

class CategoryProjectType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('categories', 'entity', array(
                'class' => 'TestProjectBundle:Category',
                'expanded' => true,
                'multiple' => true,
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('a')
                        ->where('a.type = :type')
                        ->setParameter('type', 'APP');
                },
                'label' => 'Le projet s\'inscrit-il dans le cadre d\'une APP Monde? Précisez laquelle',
            ))
        ;
    }

?>

错误

  

属性“类别”也不是其中一种方法   “getCategories()”,“isCategories()”,“hasCategories()”,“__ get()”   在课堂上存在并具有公共访问权限   “Aefe \捆绑\ ProjectBundle \实体\种类”。

1 个答案:

答案 0 :(得分:0)

更新回答:

如果我没记错的话,你的实体需要有一个ID列,而且只有一个。

试试这个,看看是否有帮助:

<?php

namespace Test\Bundle\ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /** 
     * @ORM\ManyToOne(targetEntity="Test\Bundle\ProjectBundle\Entity\Category", inversedBy="categoryprojects") 
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false) 
     */
    protected $categories;

    /** 
     * @ORM\ManyToOne(targetEntity="Test\Bundle\ProjectBundle\Entity\Project", inversedBy="categoryprojects") 
     * @ORM\JoinColumn(name="project_id", referencedColumnName="id", nullable=false) 
     */
    protected $projects;