我试图用docrtrine(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和教义还不太熟悉,我不知道它和fosuserbundle有什么关系,我做错了什么。
最佳答案
MongoDB架构应该在文档命名空间中namespace Acme\ReportsBundle\Document;