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();}