Symfony2 form and entity questions

Source: Internet
Author: User
For example, if I want to add a blog with a category, I should have a category_id field. Then, when adding a blog, I can select from the drop-down list. Q: How can I add a category list in my drop-down list! Relationship between posts and category {code ...}...

For example, if I want to add a blog with a category, I should have a category_id field. Then, when adding a blog, I can select from the drop-down list.

Question:
How can I add a category list to the drop-down list!
Relationship between posts and category

#Demo\TestBundle\Entity\PostsDemo\TestBundle\Entity\Posts:type: entitytable: postsrepositoryClass: Demo\TestBundle\Repository\PostsRepositoryid:    id:        type: integer        id: true        genertor: { strategy: AUTO }fields:    title:         type: string        length: 64    author:        type: string        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

Php app/console doctrine: generate: form DemoTestBundle: Posts generated by the command in form


  Add ('id')-> add ('title')-> add ('author')-> add ('create _ ') -> add ('Update _ at')-> add ('category ');}/*** @ param OptionsResolverInterface $ resolver */public function setDefaultOptions (OptionsResolverInterface $ resolver) {$ resolver-> setDefaults (array ('data _ class' => 'demo \ TestBundle \ Entity \ posts');}/*** form identifier, name attribute * @ return string */public function getName () {return 'posts ';}

}

This is probably the case. I don't know where to call the Code or the Code is wrong. I hope you can give me some advice.

Reply content:

For example, if I want to add a blog with a category, I should have a category_id field. Then, when adding a blog, I can select from the drop-down list.

Question:
How can I add a category list to the drop-down list!
Relationship between posts and category

#Demo\TestBundle\Entity\PostsDemo\TestBundle\Entity\Posts:type: entitytable: postsrepositoryClass: Demo\TestBundle\Repository\PostsRepositoryid:    id:        type: integer        id: true        genertor: { strategy: AUTO }fields:    title:         type: string        length: 64    author:        type: string        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

Php app/console doctrine: generate: form DemoTestBundle: Posts generated by the command in form


  Add ('id')-> add ('title')-> add ('author')-> add ('create _ ') -> add ('Update _ at')-> add ('category ');}/*** @ param OptionsResolverInterface $ resolver */public function setDefaultOptions (OptionsResolverInterface $ resolver) {$ resolver-> setDefaults (array ('data _ class' => 'demo \ TestBundle \ Entity \ posts');}/*** form identifier, name attribute * @ return string */public function getName () {return 'posts ';}

}

This is probably the case. I don't know where to call the Code or the Code is wrong. I hope you can give me some advice.

If you want to use doctrine relationship, use-> add ('product', 'entity ', array (...))
If you do not want 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 ()));}
Also add private function getChoices () {// get choices option from Entitiy or define return array (1 => '... ', 2 => '.... ');}

Put this
-> Add ('category ')
Change
-> Add ('category ', 'choice ')
Try it

-> Add ('category ', null, array ('properties' => 'title '));
-> Add ('product', 'entity ', array ('class' => 'demo \ TestBundle \ entity \ Category', 'properties' => 'title '))

The error message indicates that the _ toString () method is not implemented in the Category object. The solution is to implement this method in the Category object. You can return the items to be displayed in the drop-down list in the _ toString () method. For example, titile:

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

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.