【发布时间】:2016-03-01 12:40:53
【问题描述】:
我正在 Symfony2 中使用 mongodb 数据库制作应用程序。我对 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:“restrictions”字段必须是一个数组,但在文档 {_id:“e5a0aaa531153335963de37ccaa5f471”}
中属于 Object 类型但是文档在数据库中的限制下正确保存。
这些是我正在使用的版本:
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
【问题讨论】:
-
我不确定,但是:尝试删除默认值:'private $restrictions = array();'
标签: php mongodb symfony doctrine