我正在尝试使用学说 2 和 zend 框架 2 使用 mongodb 构建 CRUD。
我遵循了一些教程,但仍然失败。
按照链接: http: //www.bigwisu.com/2012/10/03/zend-framework-2-doctrine-odm https://github.com/doctrine/DoctrineMongoODMModule
以下是项目中使用的配置:
module.doctrine_mongodb.local.config.php
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'dbname' => 'teste',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
'default_db' => 'teste',
'filters' => array()
)
),
'documentmanager' => array(
'odm_default' => array(
// 'connection' => 'odm_default',
// 'configuration' => 'odm_default',
// 'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
),
);
module.config.php 添加
'doctrine' => array(
'driver' => array(
__NAMESPACE__.'_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Document' => __NAMESPACE__.'_driver'
)
)
)
)
文档\Artigo.php
<?php
namespace Site\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** @ODM\Document(collection="artigo") */
class Artigo
{
/** @ODM\Id */
private $id;
/** @ODM\Field(type="string") */
private $nome;
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $nome
*/
public function getNome() {
return $this->nome;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $nome
*/
public function setNome($nome) {
$this->nome = $nome;
}
}
和控制器:
<?php
namespace Site\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Site\Document\Artigo;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$artigo = new Artigo();
$artigo>setNome("teste");
$dm->persist($artigo);
$dm->flush();
return new ViewModel(array('teste'=>'acho que deu'));
}
}
没有错误发生,也没有插入记录。你知道可以是什么吗?