2012-12-14 64 views
3

我是symfony 2中的noop。有人可以幫助我獲得symfony2表單類型構建器嗎?Symfony2表單構建器實體類型選定的值

我有一個表單0123'實體,我想在表單中使用selectbox來列出我的User實體中的所有用戶。當我使用Product創建表格時,所有輸入均可用,沒有userid。用戶選擇框未選擇來自Product實體的值userId

namespace ATL\ProductBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
use Symfony\Component\Form\FormBuilderInterface; 
use Doctrine\ORM\EntityRepository; 

class ProductGeneralType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options){  
      $builder->add("name", null, array(
       "label" => "Name", 
       "required" => true, 
       "attr" => array(
        "class" => "span5", 
       ) 
      ))->add("description",null,array(
       "label" => "Description", 
       "required" => true, 
       "attr" => array(
        "class" => "span8" 
       ) 
      ))->add("visible",null,array(
       "label" => "Visible" 
      ))->add("userId","entity",array(
       "label" => "User", 
       "class" => "ATLUserBundle:User", 
       "query_builder" => function(EntityRepository $er){ 
        return $er->createQueryBuilder("u")->where("u.enabled = 1")->orderBy("u.username", "ASC"); 
       }, 
      )); 
    } 

    public function getName(){ 
     return "product"; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver){ 
     $resolver->setDefaults(array(
      "data_class" => "ATL\ProductBundle\Entity\Product" 
     )); 
    } 
} 

NameDescriptionVisible都ok,並從數據庫中獲取。但User沒有從Product實體中選擇userId。有人可以幫我弄這個嗎?

回答

1

我解決了我的問題,在我的Product實體中,我的映射名User實體是users。我將userId更改爲users,並將其修正。

相關問題