2012-09-19 47 views
2

我有朋友和類別 - 每個朋友都有一個類別。在主頁上可以看到所有朋友的列表,並且有一個包含所有類別和提交按鈕的下拉菜單 - 當您選擇類別並單擊它時,您只會看到來自該類別的朋友。期望的參數類型字符串,給定的對象 - Symfony2

這是Category.php

<?php 

namespace EM\MyFriendsBundle\Entity; 

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

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

    /** 
    * @ORM\Column(type="string", length=100) 
    * @Assert\NotBlank() 
    * @Assert\MinLength(
    *   limit=3, 
    *   message="The name is too short.") 
    */ 
    protected $name; 

    /** 
    * @ORM\Column(type="string", length=225, nullable=true) 
    */ 
    protected $description; 

    /** 
    * @ORM\ManyToOne(targetEntity="User", inversedBy="categories") 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
    */ 
    protected $user; 

    /** 
    * @ORM\OneToMany(targetEntity="Friend", mappedBy="category") 
    * @ORM\OrderBy({"name" = "ASC"}) 
    */ 
    protected $friends; 

    public function __construct() 
    { 
     $this->friends = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 
    } 

    public function getName() 
    { 
     return $this->name; 
    } 

    public function setDescription($description) 
    { 
     $this->description = $description; 
    } 

    public function getDescription() 
    { 
     return $this->description; 
    } 

    public function setUser(\EM\MyFriendsBundle\Entity\User $user) 
    { 
     $this->user = $user; 
    } 

    public function getUser() 
    { 
     return $this->user; 
    } 

    public function addFriend(\EM\MyFriendsBundle\Entity\Friend $friend) 
    { 
     $this->friends[] = $friend; 
    } 

    public function getFriends() 
    { 
     return $this->friends; 
    } 
} 

ChooseCatType.php

<?php 

namespace EM\MyFriendsBundle\Entity; 

use Symfony\Component\Form\FormBuilder; 
use Symfony\Component\Form\AbstractType; 

class ChooseCatType extends AbstractType 
{ 
    protected $user; 

    function __construct($user) 
    { 
     $this->user = $user; 
    } 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $user = $this->user; 

     $builder->add('name', 'entity', array(
      'class' => 'EMMyFriendsBundle:Category', 
      'property' => 'name', 
      'empty_value' => 'All friends', 
      'required' => false, 
      'query_builder' => function ($repository) use ($user) 
       { return $repository->createQueryBuilder('cat') 
            ->select('cat') 
            ->where('cat.user = :user') 
            ->orderBy('cat.name', 'ASC') 
            ->setParameter('user', $user); 
       },)); 
    } 

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

,這是控制器的動作:

public function filterAction(Request $request) 
    { 
     $this->init(); 

     $cat = new Category(); 

     $dd_form = $this->createForm(new ChooseCatType($this->user), $cat); 

     if($request->getMethod() == 'POST') { 
      $dd_form->bindRequest($request); 

      if($cat->getName() == null) {  
       return $this->redirect($this->generateUrl('home_display')); 
      } 

      $filter = $cat->getName()->getId(); 

      if ($dd_form->isValid()) { 
       $friends = $this->em->getRepository('EMMyFriendsBundle:Friend') 
        ->filterFriends($filter); 

       if(!$friends) { 
        $this->setFlash('There are no frieds in this category.', 'error'); 
        return $this->redirect($this->generateUrl('home_display')); 
       } 
      } 

      return $this->render('EMMyFriendsBundle:Home:filtered_home.html.twig', array(
        'friends' => $friends, 'category' => $cat->getName()->getName(), 
        'dd_form' => $dd_form->createView())); 
     } 

     return $this->redirect($this->generateUrl('home_display')); 
    } 
} 

其中init()$this->em = $this->getDoctrine()->getEntityManager();$this->user = $this->get('security.context')->getToken()->getUser();

我得到一個錯誤:Expected argument of type string, object given,但無法理解它來自哪裏以及如何解決它。請幫忙嗎?這將非常感激。我認爲它發生在這條線上$dd_form->bindRequest($request);,因爲當我在程序退出之前放入exit;時,但是當我把它放在它之後時,它顯示錯誤。

另一件事是,當我刪除

@Assert\MinLength(
     *   limit=3, 
     *   message="The name is too short.") 

這是上面Category.php name屬性,一切都運行完美。

任何想法都會有所幫助!謝謝你的時間!

還有一件事 - 爲什麼要得到我應該使用的類別的id $cat->getName()->getId();?我認爲它應該只有$cat->getId(),但事實並非如此。

+0

真的很奇怪你需要使用$ cat-> getName() - > getId();.這不應該。如果你使用$ cat-> getId(),你會得到什麼? –

+0

啊,我知道什麼是錯的。看到回答 –

+0

@Carlos格拉納多斯,我得到'getId()'沒有在對象或類似的東西上調用。 – Faery

回答

3

當你使用'實體'字段類型時,你從它得到的是一個實體,所以這個字段被稱爲「名字」是沒有意義的。因此,我將你的建設者更改爲:

$builder->add('category', 'entity', array(
... 

然後,你鴕鳥政策需要一個類別對象傳遞給該表單生成器。這通常在表單將每個字段與對象屬性連接時完成,但事實並非如此。您沒有使用此表單來獲取Category對象的「category」屬性。這只是一個表單,它會返回一個Category對象,您可以從表單數據中獲取該對象。所以改變你的控制器:

public function filterAction(Request $request) 
    { 
     $this->init(); 

     $dd_form = $this->createForm(new ChooseCatType($this->user)); 

     if($request->getMethod() == 'POST') { 
      $dd_form->bindRequest($request); 

      if ($dd_form->isValid()) { 
       $data = $form->getData(); 
       $cat = $data['category']; 

       $filter = $cat->getId(); 
       $friends = $this->em->getRepository('EMMyFriendsBundle:Friend') 
        ->filterFriends($filter); 

       if(!$friends) { 
        $this->setFlash('There are no frieds in this category.', 'error'); 
        return $this->redirect($this->generateUrl('home_display')); 
       } 
       return $this->render('EMMyFriendsBundle:Home:filtered_home.html.twig', array(
        'friends' => $friends, 'category' => $cat->getName(), 
        'dd_form' => $dd_form->createView())); 
      } 

     } 

     return $this->redirect($this->generateUrl('home_display')); 
    } 
} 
+0

非常感謝!但我產生一個錯誤:類「EM \ MyFriendsBundle \ Entity \ Category」中不存在屬性「類別」,方法「getCategory()」和方法「isCategory()」 – Faery

+0

您在哪裏得到這個? –

+0

在窗體應該顯示的頁面上,它不喜歡'$ builder-> add('category','entity',array(...' – Faery

相關問題