Doctrine-MongoDB と Symfony を使用するプロジェクトに取り組んでいます。
\@EmbedMany
注釈を使用してドキュメントを別のドキュメントに埋め込みました。
ドキュメントは次のとおりです。
MusicalInfos :
<?php
// app/Resources/Document/Musical.php
namespace AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document(collection="bv_musical_infos")
*/
class MusicalInfos
{
/**
* @MongoDB\Id(strategy="auto")
*/
protected $id;
/**
* @MongoDB\ReferenceOne(targetDocument="User")
*/
protected $user;
/**
* @MongoDB\EmbedMany(targetDocument="InstrumentsPlayed")
*/
protected $instruments = array();
そして埋め込まれたドキュメント:
<?php
namespace AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\EmbeddedDocument
*/
class InstrumentsPlayed
{
/**
* @MongoDB\ReferenceOne(targetDocument="Instruments")
*/
protected $instrument;
/**
* @MongoDB\Field(type="int")
*/
protected $practiceLevel;
その後、これらのドキュメントに記入するフォームを作成しました:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MusicalInfosType extends AbstractType
{
private $dm;
public function __construct($dm)
{
$this->dm = $dm;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('instruments', 'collection', array(
'type' => new InstrumentsPlayedType($this->dm),
'allow_add' => true,
'allow_delete' => true
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Document\MusicalInfos',
));
}
public function getName()
{
return 'musical_infos';
}
}
コントローラ
<?php
//...
if('POST' === $request->getMethod()) {
$form->bind($request);
if($form->isValid()) {
$user = $this->container->get('security.context')->getToken()->getUser();
$musicalInfos->setUser($user);
$dm->persist($musicalInfos);
$dm->flush();
$response = new JsonResponse();
$response->setData(array('registred_musical' => true));
return $response;
}
しかし、これを試みると、常にこの例外 が発生します。
どうしてか分かりません ...
ご協力いただきありがとうございます !