控制器 - class 中无访问权限 - 实体

Controller - No access in class - Entity

我收到此错误消息,但我不太明白为什么。

Neither the property "categories" nor one of the methods "addCategory()"/"removeCategory()", "setCategories()", "categories()", "__set()" or "__call()" exist and have public access in class "Checkout\Bundle\ItemBundle\Entity\Item".

问题是,在我的实体中 "Item" 我真的拥有所有这些东西:

/**
 * @ORM\ManyToMany(targetEntity="Checkout\Bundle\ItemBundle\Entity\Category", mappedBy="items")
 **/
private $categories;

/**
 * Add categories
 *
 * @param Category $categories
 * @return Item
 */
public function addCategory(Category $categories)
{
    $this->categories[] = $categories;

    return $this;
}

/**
 * Remove categories
 *
 * @param Category $categories
 */
public function removeCategory(Category $categories)
{
    $this->categories->removeElement($categories);
}

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

好的,好的 - 我的控制器里有什么?

/**
 * Creates a new Item entity.
 *
 * @Route("/create", name="item_create")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function createAction(Request $request)
{
    $entity = new Item();
    $currentUser = $this->getUser();

    $form = $this->createFormBuilder($entity)
        ->add('name', null, array(
            'label' => 'Item Name',
            'required' => true,
        ))
        ->add('categories', 'entity', array(
            'label' => 'Select a Category',
            'required' => false,
            'class' => 'CheckoutItemBundle:Category',
            'property' => 'name',
            'query_builder' => function (EntityRepository $er) use ($currentUser) {
                return $er->createQueryBuilder('c')
                    ->where('c.user = :user')
                    ->setParameter('user', $currentUser);
            },
        ))

        ->add('submit', 'submit', array('label' => 'Speichern'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();


    }

    return $this->render(
        'CheckoutItemBundle:Item:create.html.twig',
        array(
            'entity' => 'entity',
            'form' => $form->createView()
        )
    );
}

所以,最大的问题是,为什么他说他没有找到它,而它明明就在那里。任何的想法?提前致谢!

    ->add('categories', 'entity', array(
        'label' => 'Select a Category',
        'required' => false,
        'class' => 'CheckoutItemBundle:Category',
        'property' => 'name',
        'multiple' => true,
        'query_builder' => function (EntityRepository $er) use ($currentUser) {
            return $er->createQueryBuilder('c')
                ->where('c.user = :user')
                ->setParameter('user', $currentUser);
        },
    ))

您拥有多对多关系,但您的表单需要多对一关系。要修复此行为,需要将 multiple 设置为 true