例如:我想添加一篇博客,里面有一个分类,我应该有一个category_id字段,然后在添加博客的时候可以通过下拉列表选择。
疑问:
我的下拉列表中怎么才能有我添加好的分类列表呢!
posts和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
form表单时通过命令生成的: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';}
}
大概就是这样子,不知道哪里要调还是代码错误了,希望跟路大侠指教。
回复内容: 例如:我想添加一篇博客,里面有一个分类,我应该有一个category_id字段,然后在添加博客的时候可以通过下拉列表选择。
疑问:
我的下拉列表中怎么才能有我添加好的分类列表呢!
posts和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
form表单时通过命令生成的: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';}
}
大概就是这样子,不知道哪里要调还是代码错误了,希望跟路大侠指教。
如果要用到 doctrine relationship 就用 ->add('product', 'entity' , array(...))
如果不要 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 =>'....' );}
把这个
->add('category')
改成
->add('category', 'choice')
试一下看看
->add('category', null, array('property' => 'title'));
->add('product', 'entity', array( 'class' => 'demo\testbundle\entity\category', 'property' => 'title' ))
看出错提示是说category实体内没有实现__tostring()这个方法。所以解决之道就是到category实体里面去实现这个方法。你可以在__tostring()这个方法里面返回要显示在下拉列表中的东西。比如用titile:
// file: demo/testbundle/entity/category.phppublic funciton __tostring() { return $this->gettitle();}