错误"字段' xxx'必须是一个数组,但在文档"中的类型为Object;在symofny2 mongodb

时间:2016-03-01 12:40:53

标签: php mongodb symfony doctrine

我正在使用mongodb数据库在Symfony2中进行应用程序。我在Symfony和Mongodb有点新鲜。

我有一个文件部分,它对EmbedMany有字段限制。

这是代码:

class Section
{
    ....

    /** @MongoDB\EmbedMany(targetDocument="Restriction") */
    private $restrictions = array();

    public function __construct()
    {
        $this->restrictions = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add restriction
     *
     * @param DelfosBundle\Document\Restriction $restriction
     */
    public function addRestriction(\DelfosBundle\Document\Restriction $restriction)
    {
        $this->restrictions[] = $restriction;
    }

    /**
     * Remove restriction
     *
     * @param DelfosBundle\Document\Restriction $restriction
     */
    public function removeRestriction(\DelfosBundle\Document\Restriction $restriction)
    {
        $this->restrictions->removeElement($restriction);
    }

    /**
     * Get restrictions
     *
     * @return \Doctrine\Common\Collections\Collection $restrictions
     */
    public function getRestrictions()
    {
        return $this->restrictions;
    }
}

当我收到表单时,我在Section文档中添加了一个新的Restriction对象并保存文档:

$formAdd->handleRequest($request);
if ($formAdd->isValid()) {
    $dm = $this->get('doctrine_mongodb')->getManager();
    $restriction = $formAdd->getData();
    $section->addRestriction($restriction);
    $dm->persist($section);
    $dm->flush();
}

然后我在flush方法中收到下一个错误: localhost:27017:字段'限制'必须是一个数组,但在文档中是Object类型{_id:" e5a0aaa531153335963de37ccaa5f471"}

但是文档已正确保存,并带有数据库中的限制。

这些是我正在使用的版本:

david@pfc:/var/www/html/delfos$ php bin/console --version
Symfony version 2.8.3 - app/dev/debug

david@pfc:/var/www/html/delfos$ composer show -i
doctrine/annotations                 v1.2.7             Docblock Annotations Parser
doctrine/cache                       v1.6.0             Caching library offering an object-oriented API for many cache backends
doctrine/collections                 v1.3.0             Collections Abstraction library
doctrine/common                      v2.6.1             Common Library for Doctrine projects
doctrine/dbal                        v2.5.4             Database Abstraction Layer
doctrine/doctrine-bundle             1.6.2              Symfony DoctrineBundle
doctrine/doctrine-cache-bundle       1.3.0              Symfony Bundle for Doctrine Cache
doctrine/inflector                   v1.1.0             Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                1.0.5              A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                       v1.0.1             Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/mongodb                     1.2.1              Doctrine MongoDB Abstraction Layer
doctrine/mongodb-odm                 1.0.5              Doctrine MongoDB Object Document Mapper
doctrine/mongodb-odm-bundle          3.0.2              Symfony2 Doctrine MongoDB Bundle
doctrine/orm                         v2.5.4             Object-Relational-Mapper for PHP
friendsofsymfony/user-bundle         dev-master e770bfa Symfony FOSUserBundle
incenteev/composer-parameter-handler v2.1.2             Composer script handling your ignored parameter file
ircmaxell/password-compat            v1.0.4             A compatibility library for the proposed simplified password hashing algorithm: https://wiki.php.net/rfc/password_hash
jdorn/sql-formatter                  v1.2.17            a PHP SQL highlighting library
kriswallsmith/assetic                v1.3.2             Asset Management for PHP
monolog/monolog                      1.17.2             Sends your logs to files, sockets, inboxes, databases and various web services
paragonie/random_compat              v1.2.0             PHP 5.x polyfill for random_bytes() and random_int() from PHP 7
psr/log                              1.0.0              Common interface for logging libraries
sensio/distribution-bundle           v5.0.4             Base bundle for Symfony Distributions
sensio/framework-extra-bundle        v3.0.13            This bundle provides a way to configure your controllers with annotations
sensio/generator-bundle              v3.0.6             This bundle generates code for you
sensiolabs/security-checker          v3.0.2             A security checker for your composer.lock
swiftmailer/swiftmailer              v5.4.1             Swiftmailer, free feature-rich PHP mailer
symfony/assetic-bundle               v2.7.1             Integrates Assetic into Symfony2
symfony/monolog-bundle               v2.8.2             Symfony MonologBundle
symfony/phpunit-bridge               v2.8.3             Symfony PHPUnit Bridge
symfony/polyfill-apcu                v1.1.0             Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu            v1.1.0             Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring            v1.1.0             Symfony polyfill for the Mbstring extension
symfony/polyfill-php54               v1.1.0             Symfony polyfill backporting some PHP 5.4+ features to lower PHP versions
symfony/polyfill-php55               v1.1.0             Symfony polyfill backporting some PHP 5.5+ features to lower PHP versions
symfony/polyfill-php56               v1.1.0             Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70               v1.1.0             Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util                v1.1.0             Symfony utilities for portability of PHP codes
symfony/security-acl                 v2.8.0             Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle           v2.3.11            Symfony SwiftmailerBundle
symfony/symfony                      v2.8.3             The Symfony PHP framework
symfony/var-dumper                   v3.0.3             Symfony mechanism for exploring and dumping PHP variables
twig/twig                            v1.24.0            Twig, the flexible, fast, and secure template language for PHP

1 个答案:

答案 0 :(得分:3)

好的,我发现了问题。

嵌入式文档定义必须是@MongoDB \ EmbeddedDocument,我使用的是@MongoDB \ Document。

所以在我的例子中,Restrict类嵌入在Section类中必须这样定义:

/**
 * @MongoDB\EmbeddedDocument
 */
class Restriction
{
    /**
     * @MongoDB\Id(strategy="UUID")
     */
    protected $id;

    ....
}