Symfony2 form and Entity questions

Source: Internet
Author: User

For example: I want to add a blog, there is a category, I should have a category_id field, and then when adding a blog can be selected through the drop-down list.

Questions:
How can I add a categorized list to my drop-down list?
The relationship between posts and category

  #Demo \testbundle\entity\postsdemo\testbundle\entity\posts:type: Entitytable:postsrepositoryclass:demo\testbundle\repository\postsrepositoryid:id:type:integer Id:tr UE genertor: {strategy:auto}fields:title:type:string length:64 author:type:str        ing length:32 create_at:type:integer update_at:type:integermanyToOne:category:  Targetentity:category Inversedby:postsdemo\testbundle\entity\category:type:entitytable:categoryrepositoryclass:             Demo\testbundle\repository\categoryrepositoryid:id:type:integer id:true Genertor:        Strategy:AUTOfields:title:type:string length:16 Create_at:type:integer Update_at: Type:integeroneToMany:posts:targetEntity:Posts mappedby:category  

Generated by commands when form forms: Php app/console doctrine:generate:form demotestbundle:posts


  
   add('id')        ->add('title')        ->add('author')        ->add('create_at')        ->add('update_at')        ->add('category')    ;}/** * @param OptionsResolverInterface $resolver */public function setDefaultOptions(OptionsResolverInterface $resolver){    $resolver->setDefaults(array(        'data_class' => 'Demo\TestBundle\Entity\Posts'    ));}/** * 表单标示符, name属性 * @return string */public function getName(){    return 'posts';}

}

Probably this is the case, do not know where to tune or code error, I hope to follow the road warrior advice.

Reply content:

For example: I want to add a blog, there is a category, I should have a category_id field, and then when adding a blog can be selected through the drop-down list.

Questions:
How can I add a categorized list to my drop-down list?
The relationship between posts and category

  #Demo \testbundle\entity\postsdemo\testbundle\entity\posts:type: Entitytable:postsrepositoryclass:demo\testbundle\repository\postsrepositoryid:id:type:integer Id:tr UE genertor: {strategy:auto}fields:title:type:string length:64 author:type:str        ing length:32 create_at:type:integer update_at:type:integermanyToOne:category:  Targetentity:category Inversedby:postsdemo\testbundle\entity\category:type:entitytable:categoryrepositoryclass:             Demo\testbundle\repository\categoryrepositoryid:id:type:integer id:true Genertor:        Strategy:AUTOfields:title:type:string length:16 Create_at:type:integer Update_at: Type:integeroneToMany:posts:targetEntity:Posts mappedby:category  

Generated by commands when form forms: Php app/console doctrine:generate:form demotestbundle:posts


  
   add('id')        ->add('title')        ->add('author')        ->add('create_at')        ->add('update_at')        ->add('category')    ;}/** * @param OptionsResolverInterface $resolver */public function setDefaultOptions(OptionsResolverInterface $resolver){    $resolver->setDefaults(array(        'data_class' => 'Demo\TestBundle\Entity\Posts'    ));}/** * 表单标示符, name属性 * @return string */public function getName(){    return 'posts';}

}

Probably this is the case, do not know where to tune or code error, I hope to follow the road warrior advice.

If you want to use the doctrine relationship use->add (' Product ', ' entity ', Array (...))
If you don't doctrine relationship,

class PostsType extends AbstractType {    private $choices;    public function __construct( $choices)    {        $this->choices = $choices;    }    public function buildForm(FormBuilderInterface $builder, array $options)    {        ....        $builder->add('category' , 'choices' , array(        'choices' => $this->choices ,         ) );    }}
在你要实例化表单的时候public function xxxAction(){    $this->createForm( new PostsType( $this->getChoices() ) );}
同样在Action中加入private function getChoices(){    //从Entitiy获取choices选项或者自己定义    return array(1 => '...' , 2 =>'....' );}

Put this
->add (' category ')
Change into
->add (' category ', ' Choice ')
Let's try it.

->add (' category ', NULL, Array (' property ' = ' title '));
->add (' Product ', ' entity ', Array (' class ' = ' demo\testbundle\entity\category ', ' property ' = ' title ')

The error hint is that the __tostring () method is not implemented within the category entity. So the solution is to implement this method in the category entity. You can return what you want to show in the drop-down list in the __tostring () method. For example, with Titile:

// file: Demo/TestBundle/Entity/Category.phppublic funciton __toString() {    return $this->getTitle();}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.