控制器 - 类中没有访问权限 - 实体

时间:2015-05-22 12:55:51

标签: symfony doctrine-orm symfony-forms

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

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()
        )
    );
}

所以,最重要的问题是,为什么他说他没有找到它,当它显然存在时。任何的想法?提前谢谢!

1 个答案:

答案 0 :(得分:0)

    ->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