【问题标题】:Symfony2.1 persist ArrayCollection() collection to MongoDB via EmbedManySymfony2.1 通过 EmbedMany 将 ArrayCollection() 集合持久化到 MongoDB
【发布时间】:2013-01-29 05:31:24
【问题描述】:

我正在尝试使用 Doctrine (symfony2.1) 将一些数据保存到 MongoDB。这是我的实体:

// src/Acme/ReportsBundle/Entity/ReportCore.php
namespace Acme\ReportsBundle\Entity;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @MongoDB\Document(collection="registeredReports")
 */
class ReportCore {

/**
* @MongoDB\Id
*/
protected $id;

/**
* @MongoDB\String
*/
protected $title;

/**
* @MongoDB\String
*/
protected $description;

/**
* @MongoDB\String
*/
protected $reference;

/**
* @MongoDB\Date
*/
protected $created;

/**
* @MongoDB\Date
*/
protected $createdBy;

/**
* @MongoDB\EmbedMany(targetDocument="ReportFields")
*/
protected $fields = array();

public function __construct () {
    $this->fields = new ArrayCollection();
}
// Setters, getters
}

这是嵌入文档:

// src/Acme/ReportsBundle/Entity/ReportFields.php
namespace Acme\ReportsBundle\Entity;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
 *  @MongoDB\EmbeddedDocument
 */
class ReportFields {
/**
 * @MongoDB\Id
 */
protected $id;

/**
 * @MongoDB\String
 */
protected $title;

/**
 * @MongoDB\String
 */
protected $description;

/**
 * @MongoDB\String
 */
protected $name;

/**
 * @MongoDB\String
 */
protected $type;

    //Setters-getters...
}

这里是控制器:

// src/Acme/ReportsBundle/Controller/ReportsController.php
namespace Acme\ReportsBundle\Controller;
use Acme\ReportsBundle\Entity\ReportCore;
use Acme\ReportsBundle\Entity\ReportFields;
use Acme\ReportsBundle\Form\Type\ReportCoreType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ReportsController extends Controller {
    public function newAction (Request $request) {
$report = new ReportCore();
$fields1 = new ReportFields();
$report->getFields()->add($fields1);
$form = $this->createForm(new ReportCoreType(), $report);
    if ( $request->isMethod('POST') ) {
    $form->bind($request);
    if ( $form->isValid() ) {
        $dm = $this->get('doctrine_mongodb')->getManager();
    $dm->persist($report);
    $dm->flush();
    }
}
return $this->render('AcmeReportsBundle:Report:new.html.twig', array(
            'form' => $form->createView()
        ));
}

我的问题是 - 当我尝试将数据保存到数据库时,我得到了

The class 'Acme\ReportsBundle\Entity\ReportCore' was not found in the chain configured namespaces FOS\UserBundle\Document

由于我对 Symfony 和 Doctrine 还很陌生 - 我无法弄清楚它与 FOSUserBundle 有什么关系,以及我做错了什么。

【问题讨论】:

  • 您是否更新了数据库架构? php app/console doctrine:schema:update --force;
  • 我不认为是这种情况,使用 MongoDB,但我刚刚尝试过 - 它说“无需更新 - 您的数据库已经与当前实体元数据同步。”但是,错误仍然存​​在...

标签: mongodb symfony doctrine


【解决方案1】:

您的 MongoDb-Schemas 应该在 Document 命名空间中

namespace Acme\ReportsBundle\Document;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多