2016-03-18 98 views
0

排序實體類型的表格我想在symfony這樣的選擇形式,其中entrie被訂購關係(我有一個多對一的關係客戶端實體)。該表格應按企業訂購每個客戶。Symfony的:通過實體關係

這裏是我的客戶實體

/** 
* @ORM\Table(name="user") 
* @ORM\Entity(repositoryClass="MyBrowserBundle\Entity\UserRepository") 
*/ 
class User 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @ORM\ManyToOne(targetEntity="MyBrowserBundle\Entity\Enterprise", inversedBy="user", cascade={"persist"}) 
* @ORM\JoinColumn(nullable=false) 
*/ 
private $enterprise; 

//Some data 

// Setter Getter 

/** 
* Set enterprise 
* 
* @param \MyBrowserBundle\Entity\Enterprise $enterprise 
* @return User 
*/ 
public function setEnterprise(\MyBrowserBundle\Entity\Enterprise $enterprise) 
{ 
    $this->enterprise = $enterprise; 

    return $this; 
} 

/** 
* Get enterprise 
* 
* @return \MyBrowserBundle\Entity\Enterprise 
*/ 
public function getEnterprise() 
{ 
    return $this->enterprise; 
} 
public function __construct() { 
    //Some code 
} 
} 

和我的企業實體

/** 
* @ORM\Table(name="enterprise")  *@ORM\Entity(repositoryClass="MyBrowserBundle\Repository\EnterpriseRepository") 
*/ 
class Enterprise 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* 
* @ORM\Column(name="name", type="string", length=255) 
*/ 
private $name; 

/** 
* @ORM\OneToMany(targetEntity="MyBrowserBundle\Entity\User", mappedBy="enterprise", cascade={"persist"}) 
*/ 
private $user; 
/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set name 
* 
* @param string $name 
* @return Enterprise 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 
/** 
* Constructor 
*/ 
public function __construct() 
{ 
    $this->user = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

/** 
* Add user 
* 
* @param \MyBrowserBundle\Entity\User $user 
* @return Enterprise 
*/ 
public function addUser(\MyBrowserBundle\Entity\User $user) 
{ 
    $this->user[] = $user; 
    //$user->setEnterprise($this); 

    return $this; 
} 

/** 
* Remove user 
* 
* @param \MyBrowserBundle\Entity\User $user 
*/ 
public function removeUser(\MyBrowserBundle\Entity\User $user) 
{ 
    $this->user->removeElement($user); 
} 

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

我使用一個選擇類型這樣的二維數組($ fullArray是二維數組):

$formSearch = $this->createFormBuilder($fullArray) 
       ->add('List', 'choice', array(
        'choices' => $fullArray, 
        'choice_attr' => function($fullArray, $key, $index) { 
         return ['class' => 'category_'.$key]; 
        }, 
        )) 
         ->add('search', 'submit') 
         ->getForm(); 
    $formSearch->handleRequest($request); 

的probleme是當樹枝呈現的形式,所有的數據被混合。

是否有另一種方法呢? (帶的EntityType?)我是新來的Symfony 2

感謝

+0

你怎麼了填充'$ fullArray'? – craigh

+0

有兩個的foreach,但我有一個檢查的var_dump如果數組是正確的,它是。 – Zglukk

回答

0

所以,我有中庸之道換我formBuilder這樣:

$formSearch   = $this->createFormBuilder($fullArray) 
         ->add('user', 'entity', array(
          'class' => 'MyBrowserBundle:User', 
          'choice_label' => 'name', 
          'query_builder' => function (EntityRepository $er) { 
           return $er->createQueryBuilder('u') 
            ->orderBy('u.enterprise', 'ASC'); 
          }, 
          'group_by' => function($val, $key, $index) { 
           return $val->getEnterprise()->getName(); 
          }, 
          )) 
         ->add('search', 'submit') 
         ->getForm(); 

它做工精細:)