Php 将文档存储在不同的集合中-使用symfony2的Mongodb
我正在尝试使用Mongodb和symfony2将文档存储在不同的集合中 这是我的管理员,他将文档设置到数据库中Php 将文档存储在不同的集合中-使用symfony2的Mongodb,php,mongodb,collections,symfony,Php,Mongodb,Collections,Symfony,我正在尝试使用Mongodb和symfony2将文档存储在不同的集合中 这是我的管理员,他将文档设置到数据库中 public function createAction(){ $post = new Post(); $post->setUrl('A Foo Bar'); $post->setTitle('asdf'); $dm = $this->get('doctrine.odm.mongodb.document_manager'); $dm->persist($post
public function createAction(){
$post = new Post();
$post->setUrl('A Foo Bar');
$post->setTitle('asdf');
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$dm->persist($post);
$dm->flush();
return new Response('Created post id '.$post->getId());}
如您所见,这是关于
但问题是,默认情况下,它将文档创建到名为类的集合中,在我的示例中是Post(),因此集合名称是Post。我想将文档保存到一个名为charlies_posts的集合中,或者将任何字符串作为变量保存。很简单:)在文档类的定义中,只需使用“Collection”参数
Documents\User:
type: document
db: my_db
collection: charlies_post
以下是一个例子:
<?php
namespace MyProject\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document(
* collection="charlies_posts"
* )
*/
class Post
{
是的,正如您所说,我们可以将其定义为一个参数
Documents\User:
type: document
db: my_db
collection: charlies_post
在这种情况下,在YAML映射文件中,可能会选择不同的集合,但在我的情况下,我希望以友好方式设置集合名称,因为我有与用户相关的帖子,所以charlies posts应转到charlies_post集合,peter posts应转到peter_post集合
我想一定有办法设置这个,但我找不到