带有Symfony 2和MongoDB的表格
我在MongoDB中使用Symfony 2,但嵌入文档有点问题。在我的应用程序中,我有一个包含多个练习的训练(例如俯卧撑…) 以下是训练文档:带有Symfony 2和MongoDB的表格,mongodb,symfony,Mongodb,Symfony,我在MongoDB中使用Symfony 2,但嵌入文档有点问题。在我的应用程序中,我有一个包含多个练习的训练(例如俯卧撑…) 以下是训练文档: Skurty\Bundle\TestBundle\Document\Workout: fields: id: id: true date: type: date embedMany: exercices: targetDocument: Skurty\Bundle\TestBundle
Skurty\Bundle\TestBundle\Document\Workout:
fields:
id:
id: true
date:
type: date
embedMany:
exercices:
targetDocument: Skurty\Bundle\TestBundle\Document\Exercice
Skurty\Bundle\TestBundle\Document\Exercice:
fields:
id:
id: true
name:
type: string
reps:
type: hash
以及演习文件:
Skurty\Bundle\TestBundle\Document\Workout:
fields:
id:
id: true
date:
type: date
embedMany:
exercices:
targetDocument: Skurty\Bundle\TestBundle\Document\Exercice
Skurty\Bundle\TestBundle\Document\Exercice:
fields:
id:
id: true
name:
type: string
reps:
type: hash
对于表单,我使用了两种类型:
class WorkoutType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('date', 'date');
$builder->add('exercices', 'collection', array(
'type' => new ExerciceType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
));
}
/**
* Set default
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Skurty\Bundle\TestBundle\Document\Workout',
));
}
public function getName()
{
return 'workout';
}
}
及
因此,在新行动中,我使用:
public function newAction(Request $request)
{
$workout = new Workout();
$workout->setDate(new \DateTime());
$form = $this->createForm(new WorkoutType(), $workout);
return $this->render('SkurtyTestBundle:Workout:new.html.twig', array('form' => $form->createView(), 'workout' => $workout));
}
他认为:
<table>
{% for exercice in form.exercices %}
<tr>
<td>{{ form_widget(exercice) }}</td>
</tr>
{% endfor %}
</table>
$form->getData()包含:
Skurty\Bundle\TestBundle\Document\Workout Object
(
[id:protected] =>
[date:protected] => DateTime Object
(
...
)
[exercices:protected] => Doctrine\Common\Collections\ArrayCollection Object
(
[_elements:Doctrine\Common\Collections\ArrayCollection:private] => Array
(
[0] => Skurty\Bundle\TestBundle\Document\Exercice Object
(
[id:protected] =>
[name:protected] => ...
[reps:protected] => Array
(
)
)
[1] => Skurty\Bundle\TestBundle\Document\Exercice Object
(
[id:protected] =>
[name:protected] => ...
[reps:protected] => Array
(
)
)
)
)
)
如您所见,“代表”是空的,即使他们提交得很好,下面是$request:
Symfony\Component\HttpFoundation\ParameterBag Object
(
[parameters:protected] => Array
(
[workout] => Array
(
[date] => Array
(
...
)
[exercices] => Array
(
[0] => Array
(
[name] => ...
[reps] => Array
(
[0] => 5
[1] => 5
[2] => 5
)
)
[1] => Array
(
[name] => ...
[reps] => Array
(
[0] => 5
[1] => 5
[2] => 5
)
)
)
)
)
)
对于每个练习,我还有一个错误“此表单不应包含额外字段”
问题在哪里
谢谢大家! 也许你应该试试:
$builder->add('reps', 'collection', array(
'type' => 'text',
'allow_add' => true,
));
谢谢,成功了!(我可以考虑一下……)