我完全停留在Symfony Forms和Doctrine MongoDb的組合中,需要你的幫助。Symfony 2 Forms和Doctrine Mongo - 如何呈現@Embed和@Hash屬性?
我有@EmbedMany和@Hash用戶等級:
/**
* @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;
}
通過節約準則文件管理器的新紀錄後,我有這樣的結構:
{
"_id": "1",
"projects": [
{
"_id": ObjectId("50d1c5116146a13948000000"),
"name": "Project 1"
},
{
"_id": ObjectId("50d069336146a10244000000"),
"name": "Project 2"
}
],
"schedule": ["2012-12-01", "2012-12-04"]
}
此外,還有2個集合 - 項目和時間表,充滿了數據。
當我嘗試編輯用戶時,我想顯示一個包含來自這些集合和用戶所選項目的數據的2個複選框列表。 像這樣:
Example http://img607.imageshack.us/img607/8369/formj.png
的問題是如何建立這種形式@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');
}
}
或
$builder->add('schedule', 'choice', array(
'expanded' => true,
'multiple' => true,
));
$builder->add('projects', 'collection', array(
'type' => 'choice',
'options' => array(
'expanded' => true,
'multiple' => true,
),
));
有些失敗,錯誤:Expected argument of type "array", "string" given
。有些產生了成功的表現形式,但在列表中沒有選定的項目。
也許我應該使用自定義數據轉換器或手動使這些控件...
這是部分正確的。文檔可以表現爲EmbeddedDocuments,但不是其他方式。我需要的特定用例是從生成的集合中保存特定的對象快照,該集合在一段時間後過期。 – SteveB