【发布时间】:2016-11-03 16:02:34
【问题描述】:
我正在关注 this 教程,但 Symfony 似乎没有找到 MongoId 类。
{
"message": "Attempted to load class \"MongoId\" from the global namespace.\nDid you forget a \"use\" statement?",
"class": "Symfony\\Component\\Debug\\Exception\\ClassNotFoundException",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/home/local/BROCELIA/kha/dev/symfonyTraining/rest/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Id/AutoGenerator.php",
"line": 34,
"args": []
},
...
]
}
这是我的文件:
<?php
namespace Acme\StoreBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document
*/
class Product
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
*/
protected $name;
/**
* @MongoDB\Field(type="float")
*/
protected $price;
public function getId() {
return $this->id;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getName() {
return $this->name;
}
public function setPrice($price) {
$this->price = $price;
return $this;
}
public function getPrice() {
return $this->price;
}
}
这是我的控制器:(注意:我正在使用 PhPStorm,它似乎也找不到 persist() 和 flush() 方法。但不知道它是如何相关的)。
<?php
namespace Acme\StoreBundle\Controller;
use Acme\StoreBundle\Document\Product;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('AcmeStoreBundle:Default:index.html.twig');
}
public function createAction()
{
$product = new Product();
$product->setName('A Foo Bar');
$product->setPrice('19.99');
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($product);
$dm->flush();
return new Response('Created product id '.$product->getId());
}
}
我的 php 版本是 PHP 7.0.8-0ubuntu0.16.04.3 (cli) ( NTS )。我通过 pecl 安装了 php MongoDB 驱动程序。我还在我的 php.ini 末尾添加了extension=mongodb.so(我有两个:/etc/php/7.0/cli/php.ini 和/etc/php/7.0/fpm/php.ini,我都添加了它)。 php -r "phpinfo();" | grep "mongo" 输出:
mongodb
mongodb support => enabled
mongodb version => 1.1.9
mongodb stability => stable
libmongoc version => 1.3.6
mongodb.debug => no value => no value
所以我猜驱动程序已安装。虽然 php --ini; 的输出中没有 mongodb.ini(甚至 mongo.ini)这样的东西;
Configuration File (php.ini) Path: /etc/php/7.0/cli
Loaded Configuration File: /etc/php/7.0/cli/php.ini
Scan for additional .ini files in: /etc/php/7.0/cli/conf.d
Additional .ini files parsed:
/etc/php/7.0/cli/conf.d/10-mysqlnd.ini,
... Lots of .ini but nothing mongo-related.
4 小时,仍然不知道我在哪里搞砸了。我尝试并重新安装了 mongodb 驱动程序、php 和 pecl 几次,但都是徒劳的。 SO上要求这个的其他帖子似乎通过简单地重新安装驱动程序来解决他们的问题。可悲的是不是我的情况,而且我是 symfony 的新手。
【问题讨论】: