Symfony + MongoDB驱动程序问题

时间:2016-11-03 16:02:34

标签: php mongodb symfony pecl

我正在关注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的新手。

2 个答案:

答案 0 :(得分:0)

将此添加到您的实体。

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示例。

答案 1 :(得分:0)

在尝试运行composer require doctrine/mongodb-odm doctrine/mongodb-odm-bundle时,作曲家生气,告诉我,我没有mongodb扩展名。虽然我拥有它,但似乎doctrine / mongodb需要ext-mongo而不是mongodbext-mongo仅适用于php版本&lt; = 5.6。好像this package似乎可以做到这一点。