我创建了一个嵌入式表单。如何使用db?
中的名称在ItemType.php中标记1。)赞:
$builder->add('id', 'checkbox', array('label'=> $v->getName(),...
2。)顺便说一句,symfony2为每个ItemType.php呈现一个数字0,1,2。我怎么能摆脱它?
我的行动:
$task = new UserFriends();
foreach ($fb_friends as $k => $v) {
$name = $v->getName();
$friend_id = $v->getFriendId();
$id = $v->getId();
$t = new Item();
$t->name = $name;
$t->friendId = $friend_id;
$t->id = $id;
$task->getId()->add($t);
}
$form = $this->createForm(new CreateRequest(), $task, array());
CreateRequest.php-form的一部分:
$builder->add('id', 'collection', array('type' => new ItemType(),
我的ItemType.php的一部分
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', 'checkbox', array('label'=> 'Name', 'required' => false, 'mapped' => false));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Frontend\ChancesBundle\Entity\Item',
));
}
public function getName()
{
return 'item';
}
我的Item.php-Entity的一部分
class Item
{
public $id;
public $friendId;
public $name;
public function getFriendId()
{
return $this->friendId;
}
答案 0 :(得分:0)
您可以将实体管理器传递给表单,然后从数据库中选择您想要的任何内容。
在您的控制器中(或者您调用表单时):
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(new MyFormType($blabla), $blabla, array('em' => $em));
在您的表单中输入:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$somethingFromDb = $options['em']->findByName('someName');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired(array(
'em',
));
$resolver->setAllowedTypes(array(
'em' => 'Doctrine\Common\Persistence\ObjectManager',
));
}