问题
I want to use couchdb-odm in Laravel 4.2 but I keep on getting errors. The last one is:
[Semantical Error] The annotation "@Doctrine\ODM\CouchDB\Mapping\Annotations\Document" in class IO\Documents\Article does not exist, or could not be auto-loaded.
I copied mostly the sandbox/bootstrap.php and tried a couple of recommendations from previous answers in the same problem. Here is currently what I have:
My composer.json has:
"require": {
"laravel/framework": "4.2.*",
"symfony/console": ">=2.0",
"doctrine/dbal": "2.5.*@dev",
"doctrine/migrations": "1.0.*@dev",
"doctrine/common": "2.4.*",
"doctrine/couchdb": "@dev",
"doctrine/couchdb-odm": "dev-master"
},
I also attempted to put the doctrine/common in the autoload section but that did not really do anything.
The article class:
namespace IO\Documents;
use Doctrine\ODM\CouchDB\Mapping\Annotations\Document;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Document(indexed=true)
*/
class Article {}
My Controller:
$database = "test";
$httpClient = new \Doctrine\CouchDB\HTTP\SocketClient();
$resp = $httpClient->request('PUT', '/' . $database);
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
// doesn't exist so I comment out
// $reader->registerAnnotationClasses('Doctrine\ODM\CouchDB\Mapping\\');
$paths = __DIR__ . "/Documents";
$metaDriver = new \Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver($reader, $paths);
$config = new \Doctrine\ODM\CouchDB\Configuration();
$config->setProxyDir(\sys_get_temp_dir());
$config->setMetadataDriverImpl($metaDriver);
$config->setLuceneHandlerName('_fti');
$couchClient = new \Doctrine\CouchDB\CouchDBClient($httpClient, $database);
$dm = \Doctrine\ODM\CouchDB\DocumentManager::create($couchClient, $config);
$article1 = new Article();
$article1->setTitle("Who is John Galt?");
$article1->setBody("Find out!");
$dm->persist($article1);
$dm->flush();
$dm->clear();
I am still a beginner of couchdb so that adds up in the confusion.
回答1:
composer dump-autoload
did the trick.
Also, here is an update to my controller:
$annotationNs = 'Doctrine\\ODM\\CouchDB\\Mapping\\Annotations';
$couchPath = '/path/to/vendor/doctrine/couchdb-odm/lib';
\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace($annotationNs, $couchPath);
$databaseName = "test";
$documentPaths = array("IO\Documents");
$httpClient = new \Doctrine\CouchDB\HTTP\SocketClient();
$dbClient = new \Doctrine\CouchDB\CouchDBClient($httpClient, $databaseName);
$config = new \Doctrine\ODM\CouchDB\Configuration();
$metadataDriver = $config->newDefaultAnnotationDriver($documentPaths);
$config->setProxyDir(__DIR__ . "/proxies");
$config->setMetadataDriverImpl($metadataDriver);
$dm = new \Doctrine\ODM\CouchDB\DocumentManager($dbClient, $config);
$article1 = new Article();
$article1->setTitle("Who is John Galt?");
$article1->setBody("Find out!");
$dm->persist($article1);
$dm->flush($article1);
My Article class:
<?php
namespace IO\Documents;
use Doctrine\ODM\CouchDB\Mapping\Annotations as CouchDB;
/**
* @Document
*/
class Article ()
来源:https://stackoverflow.com/questions/25444853/laravel-couchdb-odm-the-annotation-doctrine-odm-couchdb-mapping-annotation