Php Symfony 2表单和Mongo-如何呈现@Embed和@Hash属性?
我完全陷入了Symfony表单和Doctrine MongoDb的组合中,需要您的帮助 我有一个带有@EmbedMany和@Hash的用户类:Php Symfony 2表单和Mongo-如何呈现@Embed和@Hash属性?,php,symfony,doctrine-orm,symfony-forms,doctrine-odm,Php,Symfony,Doctrine Orm,Symfony Forms,Doctrine Odm,我完全陷入了Symfony表单和Doctrine MongoDb的组合中,需要您的帮助 我有一个带有@EmbedMany和@Hash的用户类: /** * @MongoDB\Document */ class User { /** * @MongoDB\EmbedMany(targetDocument="Project", strategy="set") */ protected $projects; /*
/**
* @MongoDB\Document
*/
class User
{
/**
* @MongoDB\EmbedMany(targetDocument="Project", strategy="set")
*/
protected $projects;
/**
* @MongoDB\Hash
*/
protected $schedule;
}
项目类别:
/**
* @MongoDB\Document
*/
class Project
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\String
*/
protected $name;
}
Doctrine Document Manager保存新记录后,我得到了以下结构:
{
"_id": "1",
"projects": [
{
"_id": ObjectId("50d1c5116146a13948000000"),
"name": "Project 1"
},
{
"_id": ObjectId("50d069336146a10244000000"),
"name": "Project 2"
}
],
"schedule": ["2012-12-01", "2012-12-04"]
}
此外,还有两个集合-项目和时间表,充满了数据
当我试图编辑一个用户时,我想显示一个带有两个复选框列表的表单,其中包含来自这些集合的数据和用户拥有的选定项。
像这样:
问题是如何为@Embed和@Hash属性构建这样的表单
我尝试了不同的方法:
class UserFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('schedule', 'collection', array(
'type' => 'choice',
'options' => array(
'expanded' => true,
'multiple' => true,
),
));
$builder->add('projects', 'document', array(
'class' => 'Acme\MyBundle\Document\Project',
'property' => 'name',
));
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Acme\MyBundle\Document\User');
}
}
或
其中一些失败,出现错误:预期参数类型为“array”,“string”给定
。有些生成了成功的表单呈现,但列表中没有选定项
也许我应该使用自定义数据转换器或手动呈现这些控件…我解决了@Hash问题。考虑到数字索引数组,应改用@Collection注释
$builder->add('schedule', 'choice', array(
'choices' => <your choices list here>,
'expanded' => true,
'multiple' => true,
));
$builder->add('schedule','choice',数组(
“选择”=>,
“扩展”=>true,
“多个”=>true,
));
@EmbedMany问题仍然存在。要使用嵌入式文档,必须将嵌入式文档注释为EmbeddedDocument,而不是document。但是,看起来您实际上希望在用户文档中的项目注释上使用ReferenceMany;从嵌入文档列表中进行选择是没有意义的,除非您正在选择要删除的文档。这部分是正确的。文档可以表现为嵌入文档,但不能表现为嵌入文档。我需要的一个特定用例是保存生成的集合中的特定对象快照,该集合将在一段时间后过期。
$builder->add('schedule', 'choice', array(
'choices' => <your choices list here>,
'expanded' => true,
'multiple' => true,
));