使用 DoctrineODM 从 CouchDb 中检索文档

Retrieve Document form CouchDb With DoctrineODM

我正在使用这个 bundle 来管理我的数据

我可以创建文档,但无法检索它们 我试过

    $dm = $this->container->get('doctrine_couchdb.odm.default_document_manager');
    $users = $dm->getRepository('myGarageBundle:Utente')->find("781f1ea2e6281beb4ee9ff72b6054af2");

这会检索一个这样的文档:

object(my\GarageBundle\CouchDocument\Utente)[303]
  private 'id' => string '781f1ea2e6281beb4ee9ff72b6054af2' (length=32)
  private 'name' => string 'foo' (length=7)

没关系。 但如果我这样做

$users = $dm->getRepository('myGarageBundle:Utente')->findBy(array('name' => 'foo'));

我有一个空数组。

我在 couchDb 中的文档是

{"_id":"781f1ea2e6281beb4ee9ff72b6054af2","_rev":"1-f89fc2372709de90ab5d1f6cfe6a8f47","type":"my.GarageBundle.CouchDocument.Utente","name":"foo"}

勾选这个page

Querying by simple conditions only works for documents with indexed fields.

你必须在你的实体中添加这个

/**
 * @CouchDB\Index
 * @CouchDB\Field(type="string")
 */
private $name;

The Doctrine persistence interfaces ship with a concept called ObjectRepository that allows to query for any one or set of fields of an object. Because CouchDB uses views for querying (comparable to materialized views in relational databases) this functionality cannot be achieved out of the box. Doctrine CouchDB could offer a view that exposes every field of every document, but this view would only grow into infinite size and most of the information would be useless.

如果你想使用像findAll()这样的方法,你必须索引所有你的文档:

<?php
/** @Document(indexed=true) */
class Person
{
    /**
     * @Index
     * @Field(type="string")
     */
    public $name;
}