Symfony + MongoDB 驱动问题

Symfony + MongoDB driver issue

我正在学习 this 教程,但 Symfony 似乎没有找到 MongoId class。

{
    "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 的新手。

将此添加到您的实体。

use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;

所以:

namespace Acme\StoreBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;

/**
 * @MongoDB\Document
 */
class Product
{
   /**
    * @MongoDB\Id
    */
   protected $id;

编辑

这就是您在应用中安装它的方式。

Composer.json

{
    "require": {
        "doctrine/mongodb-odm": "1.0.*@dev",
        "doctrine/mongodb-odm-bundle": "3.0.*@dev"
    },
}

作曲家更新

php composer.phar update doctrine/mongodb-odm doctrine/mongodb-odm-bundle

Autoload.php

# app/autoload.php
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
AnnotationDriver::registerAnnotationClasses();

AppKernel.php

new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),

配置

# app/config/config.yml
doctrine_mongodb:
    connections:
        default:
            server:  mongodb://localhost:27017
            options: {}

    default_database: your_mongodb_database
    document_managers:
        default:
            auto_mapping:  true
            retry_connect: 3
            retry_query:   3

确保你的 mongo 是 运行

$ mongod

查看 Simple doctrine mongodb CRUD example in symfony 示例。

在尝试 运行 composer require doctrine/mongodb-odm doctrine/mongodb-odm-bundle 时,作曲家很生气,告诉我我没有 mongodb 扩展名。虽然我有,但似乎 doctrine/mongodb 需要 ext-mongo 而不是 mongodbext-mongo 仅适用于 php 版本 <= 5.6。不过,似乎 this package 可以解决这个问题。