douchuanchai2793
2016-11-03 16:02
浏览 89
已采纳

Symfony + MongoDB驱动程序问题

I'm following this tutorial, but Symfony doesn't seem to find the MongoId class.

{
    "message": "Attempted to load class \"MongoId\" from the global namespace.
Did 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": []
      },
      ...
    ]
}

Here's my Document:

<?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;
}
}

And here's my Controller: (NB: I'm using PhPStorm and it doesn't seem to find the persist() and flush() methods either. Don't know how it could be related though).

    <?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());
        }
    }

My php version being PHP 7.0.8-0ubuntu0.16.04.3 (cli) ( NTS ). I installed the php MongoDB driver through pecl. I also added extension=mongodb.so at the end of my php.ini (I had two of these: /etc/php/7.0/cli/php.ini and /etc/php/7.0/fpm/php.ini, I added it in both). php -r "phpinfo();" | grep "mongo" outputs this:

mongodb
mongodb support => enabled
mongodb version => 1.1.9
mongodb stability => stable
libmongoc version => 1.3.6
mongodb.debug => no value => no value

So I'm guessing the driver is installed. Although there's no such thing as mongodb.ini (or even mongo.ini) in the output of php --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 hours and still no idea of where I messed up. I tried and reinstalled the mongodb driver, php and pecl several times, in vain. The other posts on SO asking for this seem to have fixed their issues by a simple reinstall of the driver. Sadly wasn't my case, plus I'm new into symfony.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyi1341 2016-11-04 08:46
    已采纳

    While trying to run composer require doctrine/mongodb-odm doctrine/mongodb-odm-bundle, composer was red-angry, telling me I don't have the mongodb extension. Although I have it, it seems that doctrine/mongodb requires ext-mongo and not mongodb. ext-mongo only works for php versions <= 5.6. Seems like this package might be able to do the trick though.

    点赞 打赏 评论
  • duansao20000508 2016-11-03 16:20

    Add this to your entity.

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

    So:

    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;
    

    EDIT

    This is how you install it in your app.

    Composer.json

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

    Composer update

    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(),
    

    Configuration

    # 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
    

    Make sure your mongo is running

    $ mongod
    

    Look at Simple doctrine mongodb CRUD example in symfony for the example.

    点赞 打赏 评论

相关推荐 更多相似问题