使用DoctrineMongoDB持久化嵌入文档时出错:给定数组

时间:2015-03-16 15:55:33

标签: php symfony doctrine doctrine-mongodb

我正在使用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;
                        }

但是当我尝试这个时,我总是得到这个例外: 警告:spl_object_hash()期望参数1为对象,给定数组

我不知道为什么......

感谢您的帮助!

0 个答案:

没有答案