2015-05-22 80 views
0

我得到了這個錯誤消息,但我不明白爲什麼。控制器 - 沒有訪問類 - 實體

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

的事情是,在我的實體 「項」 我真的有這一切東西:

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

所以,最大的問題是,爲什麼他說他沒有找到它,當它顯然存在。任何想法?先謝謝你!

+0

你的映射沒有錯誤嗎?你可以用'doctrine:schema:validate'來檢查它。這將確保它不是你的ManyToMany關係給我帶來麻煩,就像我懷疑的那樣。 –

+0

感謝您的快速響應和幫助。我已經檢查過這方面,沒有錯誤。 :-) – tokuimo

+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

+0

是的,就是這樣!謝謝! :-) – tokuimo