GithubHelp home page GithubHelp logo

doctrine / mongodb-odm Goto Github PK

View Code? Open in Web Editor NEW
1.1K 49.0 501.0 11.79 MB

The Official PHP MongoDB ORM/ODM

Home Page: https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/

License: MIT License

PHP 99.83% Shell 0.17%
mongodb odm orm php symfony

mongodb-odm's Introduction

Doctrine MongoDB Object Document Mapper

Build Status Code Coverage Gitter

The Doctrine MongoDB ODM project is a library that provides a PHP object mapping functionality for MongoDB.

More resources:

mongodb-odm's People

Contributors

alcaeus avatar asafdav avatar avalanche123 avatar beberlei avatar bilge avatar bmichalski avatar caciobanu avatar carusogabriel avatar clslrns avatar djlambert avatar franmomu avatar greg0ire avatar ionbazan avatar j avatar jmikola avatar jwage avatar kriswallsmith avatar malarzm avatar megazoll avatar ocramius avatar olvlvl avatar ornicar avatar pborreli avatar petr-buchin avatar pgodel avatar senseexception avatar spantaleev avatar stof avatar superdweebie avatar watari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongodb-odm's Issues

Using YAML description with embedMany causes PHP notice error

When using the YAML style description together with the new embedMany functionality of mixed documents, you get a PHP notice error every time you create a new model.

Because when you have mixed documents, you don't specify a targetDocument, and therefore the YAML Driver can not find an element in the array that describes the targetDocument.

sort('id', 'asc')

The following query only works if I put an 'underscore' before 'id':

            $query = $manager->createQueryBuilder('iMARKET\GeoBundle\Document\ServiceAreaMap')
                ->sort('_id', 'asc')
                ->getQuery();

It should be:

            $query = $manager->createQueryBuilder('iMARKET\GeoBundle\Document\ServiceAreaMap')
                ->sort('id', 'asc')
                ->getQuery();

Andras

Cannot load documents with embdedded files

Attempting to load a collections of documents with embedded files (MongoGridFSFile) will results in the following error:

 Fatal error: Cannot use object of type MongoGridFSFile as array in Doctrine/ODM/MongoDB/UnitOfWork.php on line 2267

The $data variable in UnitOfWork::getOrCreateDocument is expected to be an array but instead is given an instance of \MongoGridFSFile.

I temporarily fixed the issue by adding the following to the getOrCreateDocument() method:

if($data instanceof \MongoGridFSFile){
    $data = $data->file;
}

... however, I'm sure there is a better way to fix the issue.

To replicated, create an Image document like in the Reference Guide, in Storing files with MongoGridFS. Add some records and then try and load them in any fashion, like findAll() on the repository class.

Do not add '$db' in reference when referencing document from same $db

When using ReferenceOne or ReferenceMany , doctrine will create fully-qualified reference to the object (with all fields: '$id', '$ref', '$db') even when reference is within same $db.

This affects maintenance and some special cases, since you cannot copy / rename database without additional overhead.

Is it possible to omit $db part when referencing document from same db?

Embedded documents update error.

Hi,
I found this weird issue where by I added another embedded document into a existing document it break down my embedded into two parts.

Here's an example class
PurchaseOrder
-———————————

 
/**
 * @MongoDB\Document(collection="PurchaseOrder")
 */
class PurchaseOrder
{
/**
    /**
    * @MongoDB\Id(strategy="auto")
    */
    protected $id;
    /**
    * @MongoDB\EmbedMany(targetDocument="Order")
    */
    protected $purchaseOrder;

    /**
     * Add order
     *
     * @param Cereals\ProductBundle\Document\Order\Order $order
     */
    public function addPurchaseOrder(\Cereals\ProductBundle\Document\Order\Order $order)
    {
        $this->order[] = $order;
    }

    /**
     * Get order
     *
     * @return Doctrine\Common\Collections\Collection $order
     */
    public function getOrder()
    {
        return $this->order;
    }
}

Order
-———————————

/**
 * @MongoDB\EmbeddedDocument
 */
class Order
{
    /**
     * @MongoDB\ReferenceOne(targetDocument="Test\VendorBundle\Document\Vendor")
     */
    protected $vendor;
    /**
    * @MongoDB\String
    * @var String
    */
    protected $vendorName;
    /**
     * @MongoDB\EmbedMany(targetDocument="OrderItem")
     */
    protected $orderItems=array();
    /**
     * Add orderItems
     *
     * @param Cereals\ProductBundle\Document\Order\OrderItem $orderItems
     */
    public function addOrderItems(\Cereals\ProductBundle\Document\Order\OrderItem $orderItems)
    {
        $this->orderItems[] = $orderItems;
    }

    /**
     * Get orderItems
     *
     * @return Doctrine\Common\Collections\Collection $orderItems
     */
    public function getOrderItems()
    {
        return $this->orderItems;
    }
} 

I'm running Symfony2. So here's an what I run with the code below.
-———————————

$dm = $this->get('doctrine.odm.mongodb.document_manager');

$purchaseOrder = new PurchaseOrder();
$order = new Order();
$order->setVendorName(".....");
$orderItem = new OrderItem();

$order->addOrderItem($orderItem);
$purchaseOrder->addOrder($order);
$dm->persist($purchaseOrder);
$dm->flush();

The above give the result below, which is right.

db.PurchaseOrder.update({ "_id": ObjectId("4e3ed35d4fcfa90a08020000") }, { "$pushAll": { "order": [ { "vendor": { "$ref": "Vendor", "$id": ObjectId("4e3eb4a04fcfa9c005070000"), "$db": "test" }, "vendorName": "drugCompany" } ] } });
db.PurchaseOrder.update({ "_id": ObjectId("4e3ed35d4fcfa90a08020000") }, { "$pushAll": { "order.0.orderItems": [ {"productName": "Bifen Suspension", "productId": ObjectId("4e3eb4a04fcfa9c005090000") } ] } });

But If i add another embedded document with this code.

$dm = $this->get('doctrine.odm.mongodb.document_manager');
$purchaseOrder = $dm->getRepository('TestProductBundle:PurchaseOrder')->find("4e3ed35d4fcfa90a08020000");

$order = new Order();
$order->setVendorName(".....");
$orderItem = new OrderItem();

$order->addOrderItem($orderItem);
$purchaseOrder->addOrder($order);
$dm->persist($purchaseOrder);
$dm->flush();

It give a wrong order of execution of query.

db.PurchaseOrder.update({ "_id": ObjectId("4e3ed35d4fcfa90a08020000") }, { "$pushAll": { "order.1.orderItems": [ { "productName": ".....", "productSn": "....", "productId": ObjectId("4e3eb4a04fcfa9c0050a0000") } ] } });
db.PurchaseOrder.update({ "_id": ObjectId("4e3ed35d4fcfa90a08020000") }, { "$pushAll": { "order": [ { "vendor": { "$ref": "Vendor", "$id": ObjectId("4e3eb4a04fcfa9c005080000")}, "vendorName": "Health" } ] } });

Under PurchaseOrder embedded document for order has the follow.

  1. A embedded Order with vendorName and OrderItem.
  2. A embedded Order with OrderItem only
  3. A embedded Order with vendorName.

Single Collection Inheritance mapping does NOT work if the subclasses are in a different namespace

Problem Description

  • I have a parent class Exchange and a subclass PersonalMessage in two different namespaces.
  • I create an instance of the PersonalMessage class and persist it using the document manager.
  • The document is successfully persisted, BUT _the discriminator field is NOT SET_ in the database. (bug)
  • As the discriminator field is not set, while finding the document and returning, obviously, the instance created does not belong to the subclass but of the parent class instead. (In my case, the parent class is abstract, but that doesn't make a difference.)
  • If I move BOTH the classes into the SAME NAMESPACE (be it any), the 'type' field is set while persisting the document. Everything works fine. This definitely means it's an issue with them being in separate namespaces.

Additional Info (may not be useful)

  • If I go directly into the database and try to retrieve a document of the parent type, it perfectly resolves the discriminator field and returns the document of the appropriate subclass instead.

My Example Code

Here's my example parent document and inherited document in two different namespaces:

_My Parent Class in the '\Document' namespace_

<?php
namespace Document;

/**
 *
 * @abstract
 * @Document
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField(fieldName="type")
 * @DiscriminatorMap({"p"="\Document\Exchange\PersonalMessage","s"="\Document\Exchange\Share"})
 */
abstract class Exchange
{
    /**
     * @Id
     */
    protected $id;

    /**
     * Time of exchange
     *
     * @var \Datetime
     * @Date
     */
    protected $time;

    public function setTime($time)
    {
        $this->time = $time;
    }
}

_Example subclass in the '\Document\Exchange' namespace_

<?php
namespace Document\Exchange;

/**
 * Document for Personal Messages (extended from Exchange document)
 *
 * @Document
 */
class PersonalMessage extends \Document\Exchange
{
    /**
     * @var String
     * @String
     */
    protected $foo;


    public function setFoo($foo)
    {
        $this->foo = $foo;
    }
}

Perform mapreduce without trying to find / retrieve results?

I'm trying to perform a mapreduce in which the output is stored to another collection. I've set the configuration for the 'out' option. The mapreduce works fine when I run execute, but what seems to happen is that it also tries to _find / retrieve_ the results from the results collection after executing the map/reduce. This is an unnecessary overhead.

Ideally, it should simply run the mapreduce and do nothing or return the statistics of the mapreduce (time taken, emits, etc). Instead, it queries for all the results of the mapreduce.

<?php 

// $qb is the query builder

$qb->map('function(){
            emit(this.from.userId, {
                sex: this.from.sex,
                circle: this.from.circle,
                age: this.from.age
            });
        }')
        ->reduce('function(k, values){
            return values[values.length - 1];
        }')
        ->out(array('replace' => 'tmp.mr.ActiveUsers_foo'));

$query = $qb->getQuery();

var_dump($query->execute()); // is a LoggableCursor, why?

If I log the commands / queries executed, there's a MapReduce command _followed by a "find" on the collection tmp.mr.ActiveUsers_foo_. The second query shouldn't happen. Is this an intentional behavior? If so, how do I prevent it from happening?

Random hydration problem

I have the following collection named Isboo_Item :
{
"_id": ObjectId("4d3d02fec9fa877d44a36978"),
"author": null,
"backpack": null,
"itemType": 1,
"owner": "4d3cf450b532519765080000",
"place": null,
"quality": null,
"quantity": 1,
"value": null
}

and have a PHP class :

/**
@document(db="Isboona", collection="Isboo_Item")
*/
class Isboo_Item extends Isboo_Db
{
}
(...)

When I'm trying :
$dm->findOne($collection, array('_id' => $id));

I get :

Fatal error: Uncaught exception 'ReflectionException' with message 'Class 4 does not exist' in /users/developpement/www/library/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php:285

However the same works fine with another collection :

{
"_id": ObjectId("4d3cf450b532519765080000"),
"characs": {
"mana": 64,
"strenght": 83,
"agility": 69,
"dexterity": 88,
"constitution": 94,
"culture": 79,
"vivacity": 57,
"eloquence": 64,
"intellect": 74,
"willpower": 88,
"empathy": 96,
"charisma": 83
},
"name": "Krok",
"position": {
"IDplace": 0,
"posX": 0,
"posY": 0,
"posLocalX": 0,
"posLocalY": 0
},
"refFather": {
"$ref": "Isboo_Character",
"$id": ObjectId("4d3cf450b532519765090000"),
"$db": "Isboona"
},
"refMother": {
"$ref": "Isboo_Character",
"$id": ObjectId("4d3cf450b5325197650a0000"),
"$db": "Isboona"
}
}

Circulare references

I have some problems wth circulare references. I have a Post and Category enties. So when i get the Post i get the post and his categories but when i get a category it has no posts.

example enties
namespace Documents\Blog;

/**

  • @document(
  • db="test_database",
  • collection="blog_posts",
  • indexes={
  • @Index(keys={"createdAt"="desc"})
    
  • }
  • )
    /
    class Post
    {
    /
    *
    • @id
      /
      public $id;
      /
      *
    • @string
      /
      public $title;
      /
      *
    • @string
      /
      public $body;
      /
      *
    • @Date
      /
      public $createdAt;
      /
      *
    • @ReferenceMany(targetDocument="Category", cascade="all")
      */
      public $categories = array();

}

namespace Documents\Blog;

/**

  • @document(
  • db="test_database",
  • collection="blog_categories",
  • indexes={
  • @Index(keys={"title"="desc"}, options={"unique"=true})
    
  • }
  • )
    /
    class Category
    {
    /
    *
    • @id
      /
      public $id;
      /
      *
    • @string
      /
      public $title;
      /
      *
    • @Date
      /
      public $createdAt;
      /
      *
    • @ReferenceMany(targetDocument="Post", cascade="all")
      */
      public $posts = array();
      }

ClassMetadataInfo::getIdentifierValue($document) don't work with a Doctrine Proxy Document

ClassMetadataInfo::getIdentifierValue($document) use property reflection to retrieve the document identifier property value.

This is possible because ClassMetadata::mapField call setAccessible(true) on all $document properties.

When $document is a Doctrine\ODM\MongoDB\Proxy\Proxy that hasn't been loaded yet, all the document properties are empty and getIdentifierValue return NULL.

It should be the same with ClassMetadataInfo::getFieldValue that also use reflection property.

Access to undeclared static property: Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::

I'm facing this critical issue on all my Documents which have either an @odm\UniqueIndex, @odm\InheritanceType, @odm\DiscriminatorField, etc field defined for. Basically, if there's anything more than an @odm\Document annotation for the class, I get the error below while trying to retrieve a document using the DocumentManager:

$dm->find('Document\User', '4e283aabc2749a4b7d0001dd')

Document Code

<?php
namespace Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document
 * @ODM\UniqueIndex(keys={"mobile"="asc"})
 */
class User extends \Document\AbstractDocument
{
    /**
     * @var string
     * @ODM\Id
     */
    protected $id;

    /**
     * @var string
     * @ODM\String
     */
    protected $mobile;

}

Error

Fatal error: Access to undeclared static property: Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::$�S�� in /home/dayson/projects/www/textme/ninja/library/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php on line 146

Call Stack
#   Time    Memory  Function    Location
.....
6   0.0037  1319032 Doctrine\ODM\MongoDB\DocumentManager->find( )   ../Test.php:34
7   0.0038  1322736 Doctrine\ODM\MongoDB\DocumentManager->getRepository( )  ../DocumentManager.php:574
8   0.0038  1322736 Doctrine\ODM\MongoDB\DocumentManager->getClassMetadata( )   ../DocumentManager.php:474
9   0.0038  1322736 Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory->getMetadataFor( )    ../DocumentManager.php:273
10  0.0038  1322736 Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory->loadMetadata( )  ../ClassMetadataFactory.php:180
11  0.0050  1495432 Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver->loadMetadataForClass( )   ../ClassMetadataFactory.php:243

For some reason it's trying to access a static property with weird characters like $D7��1, etc.

What's happening? Could someone please help me resolve this as we've hit a roadblock in our project due to this. The latest master branch is being used.

"Query" Logger

Please add query logging support. I need for analysis and optimization.

The latest commit b98a1dbf92fbd9c43904 broke mongo-odm with symfony2 RC4

Hi,

if you setup mongodb-odm with symfony2 rc4 the annotation classes are not correctly autoloaded even you add the

Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::registerAnnotationClasses();

to autoload.php. For some reason the registered custom autoload function is not triggered and the annotation classes are not loaded.

In AnnotationDriver::loadMetadataForClass():

$this->reader->getClassAnnotations($reflClass) returns an incomplete class.

This is happens, because the FQN of the Annotation is mapped to vendor/doctrine-mongodb-dm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/xxxxAnnotationClassNamexxxxx.php.

whereas the annotation class definitions are in a single file!

Error converting datefield value

Seems to only happen on windows OS, here is the error:

http://grab.by/6ttp

Happens when you try to convert this date:

converting DateTime Object ( [date] => 1890-01-01 00:00:00 [timezone_type] => 3 [timezone] => UTC )

Which converts to false and then is passed to the MongoDate object.

Changing DataType.php function to this fixes the issue:

public function convertToDatabaseValue($value)
{
    if ($value instanceof \DateTime) {
        $value = $value->getTimestamp();
    }
    if (is_string($value)) {
        $value = strtotime($value);
    }
    if (!$value) {
        return null;
    }
    return new \MongoDate($value);
}

[query builder] using select('aField') locks future queries to that field

First query properly returns only the week field

        $result = $dm->createQueryBuilder(__CLASS__)
            ->select('aField')
            ->getQuery()->getSingleResult();

Second query:

        $result = $dm->createQueryBuilder(__CLASS__)
            ->select('anotherField')
            ->getQuery()->getSingleResult();

When ran after the first query, the return document only selected 'aField'. When ran standalone it properly selected 'anotherField'

This is using ALPHA3 from PEAR. Let me know if I can help by providing any further info.

Support for the ObjectId / MongoId type for a field

Presently, there's no support for storing the ObjectId (aka MongoId) type in a field. This makes it difficult to maintain references manually. I have the option of storing the _id I want to reference as a @string (for e.g - "4e18e625c2749a260e000024" ), but that takes twice the amount of space for the field as well as the indexes.

I believe there's a strong need to support the @ObjectId type just like how the MongoDate and MongoBinData are supported. Another solution would be to just allow support for the @Id type to be used multiple times.

An example temporary implementation would be like below:

/**
 * Custom Data type to support the MongoId data type in fields
 */
class ObjectId extends \Doctrine\ODM\MongoDB\Mapping\Types\Type
{
    public function convertToDatabaseValue($value)
    {
        if ($value === null) {
            return null;
        }
        if ( ! $value instanceof \MongoId) {
            $value = new \MongoId($value);
        }
        return $value;
    }

    public function convertToPHPValue($value)
    {
        return $value !== null ? (string)$value : null;
    }
}

There are a lot more use cases for NOT using DBRef than to use it. You can read more about the advantages here:

mongodb:generate:documents adds duplicate properties/getters/setters on subclass when using inheritance

Originally created at https://github.com/symfony/DoctrineMongoDBBundle/issues/39

Note: this "bug" was found using Symfony2.

Start with these two Documents:


namespace My\XBundle\Document;

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

/**
 * @MongoDB\Document
 * @MongoDB\InheritanceType("COLLECTION_PER_CLASS")
 */
abstract class AbstractFoo
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $displayName;
}

namespace My\XBundle\Document;

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

/**
 * @MongoDB\Document
 */
class SubFoo extends AbstractFoo
{
    /**
     * @MongoDB\String
     */
    protected $value;
}

Now, when you run mongodb:generate:documents My\XBundle, you will end up with this in the subclass:


namespace My\XBundle\Document;

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

/**
 * @MongoDB\Document
 */
class SubFoo extends AbstractFoo
{
    /**
     * @MongoDB\String
     */
    protected $value;

    /**
     * @var $id
     */
    protected $id;

    /**
     * @var string $displayName
     */
    protected $displayName;


    /**
     * Set value
     *
     * @param string $value
     */
    public function setValue($value)
    {
        $this->value = $value;
    }

    /**
     * Get value
     *
     * @return string $value
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set displayName
     *
     * @param string $displayName
     */
    public function setDisplayName($displayName)
    {
        $this->displayName = $displayName;
    }

    /**
     * Get displayName
     *
     * @return string $displayName
     */
    public function getDisplayName()
    {
        return $this->displayName;
    }
}

Index annotation is not used properly

I have a problem with DoctrineMongoDBBundle
I have a document in my bundle that works fine without Index
but when I add index to the document's field , the app/console doctrine:mongodb:generate:documents throws exception

thanks
Saleh

<?php
namespace <Bundle>\Document;
use \Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
 * @MongoDB\Document
 */
class Users
{
    /**
     * @MongoDB\Id
     */
    protected $id;
    /**
     * @MongoDB\Int @MongoDB\Index
     */
    public $uid;
    ...

the error


  [ErrorException]                                                                                                                                                                 &nbs p;                                                                                                                                                                    &nbs p;                                                                                                                                                                    &nbs p;     
  Catchable Fatal Error: Argument 1 passed to Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::addIndex() must be an instance of Doctrine\ODM\MongoDB\Mapping\ClassMetadata, instance of Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo given, called in <PATH>/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php on line 226 and defined in <PATH>/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php line 260  
                                                                                                                                                                    &n bsp;                                                                                                                                                                    &n bsp;                                                                                                                                                                    &n bsp;                    

doctrine:mongodb:generate:documents [--document[="..."]] bundle

Unexpected result when deleted embedded collections

Scenario: Before deleting an embedded collection, the original collection shall be backuped.

The script is listening for the onFlush Event.

For Updates this works fine:

foreach ($uow->getScheduledCollectionUpdates() AS $col) {
    $col->getSnapshot(); # gives me the original collection -> works as expected
}

However this does not work for Deletions:

foreach ($uow->getScheduledCollectionDeletions() AS $col) {
    $col->getSnapshot(); # is allways empty. -> does NOT work as expected

    # Also empty (which I consider to be really strange: as OriginalDocumentData is documented as "The original data is the data that was present at the time the document was reconstituted from the database"):

    $org = $uow->getOriginalDocumentData( $col->getOwner() );
    $mapping = $col->getMapping();
    $org[$mapping["fieldName"]]; # empty; but it should have the original collection, which is going to be deleted
}

Any Ideas?

dbref $id persisted as string instead of objectid

I've noticed this when playing around with the sandbox and references:

(output of mongo shell)

db.testcollection.findOne().references[0]
{ "$ref" : "testreference", "$id" : "4c3f0b456cc8849c17ab0000" }

The $id of the dbref here is a string, but I would expect it to be an ObejctId (a MongoId in PHP), like so:

db.testcollection.findOne().references[0]
{ "$ref" : "testreference", "$id" : ObjectId("4c3f0b456cc8849c17ab0000") }

This allows for...

db.testcollection.findOne().references[0].fetch()
{ ... referenced document here ... }

I couldn't find a note if this was intended by the odm or not, but an ObjectId is the proper format, here:

http://www.mongodb.org/display/DOCS/Database+References

ErrorException when generating from yaml - ClassMetadata vs. ClassMetadataInfo

If I try simple tutorial http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

then in step "Generating Getters and Setters" I get:

[ErrorException]

Catchable Fatal Error: Argument 1 passed to Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver::addFieldMapping() must be an instance of Doctrine\ODM\MongoDB\Mapping\ClassMetadata, instance of Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo given, called in /z/public_html/mongo/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Driver/YamlDriver.php on line 104 and defined in /z/public_html/mongo/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Driver/YamlDriver.php line 136

If I change ClassMetadata into ClassMetadataInfo in YamlDriver.php, it works.
But because of:

use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;

I don't know which occurences of ClassMetadata are in purpose...

Provide access to the result object after executing MapReduce command

When a MapReduce is performed in MongoDb, it returns some very useful and important statistics about the command execution like time taken, input, emit, output (look at the example output below). The ODM has completely hidden access to this result object. It would be a good feature to give access to this result object.

Read more on result object here: http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Resultobject

Example Result Object

array
  'result' => string 'tmp.mr.ActiveUsers_foo' (length=22)
  'timeMillis' => int 194
  'counts' => 
    array
      'input' => int 1431
      'emit' => int 1431
      'output' => int 1064
  'ok' => float 1

id generator strategy not working?

I'm trying to use strategy INCREMENT to create numeric identifiers, but every time I get MongoDB id presentation.

Code is:

/**
* @odm\Id(strategy="INCREMENT")
*/
private $id;

Maybe I missing something or this is a bug in current development version?

repositoryClass attribute of the Document annotation

It looks like the repositoryClass isn't being picked up anymore - so functions on custom Repositories for a document aren't available.

Not 100% sure but I suspect the problem is in ClassMetadataFactory::getMetadataFor

An instance of ClassMetadata wanted instead of ClassMetadataInfo in Event/LoadClassMetadataEventArgs.php

Because of this error i'm unable to generate entities with Doctrine ODM (coupled with Symfony 2) :

~/Documents/workspace/winiti(master) $ php app/console doctrine:mongodb:generate:documents MyBundle

  [ErrorException]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  Catchable Fatal Error: Argument 1 passed to Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs::__construct() must be an instance of Doctrine\ODM\MongoDB\Mapping\ClassMetadata, instance of Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo given, called in /Users/dunglas/Documents/workspace/winiti/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php on line 273 and defined in /Users/dunglas/Documents/workspace/winiti/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Event/LoadClassMetadataEventArgs.php line 41  


doctrine:mongodb:generate:documents [--document[="..."]] bundle

Incorrect assignment of special field names

If I want a specific field name you want in a property, he always removed from the query.
Example:

/** @Document(collection="users") */
class User {
    /** @Id */
    protected $id;
    /** @String(name="usernameee") */
    protected $username;

    // ...
}

$user = new User();
$user->setUsername("Tecbot");
$dm->persist($user);
$dm->flush();

username does not exist then the query!!

Error could be corrected:

Class: ClassMetadata
Function: mapField

public function mapField(array $mapping)
{
    $fieldName = $mapping['fieldName']; // Save fieldName tmp
    //...

    if ($this->reflClass->hasProperty($mapping['fieldName']) {
        $reflProp = $this->reflClass->getProperty($mapping['fieldName']);
        $reflProp->setAccessible(true);
        $this->reflFields[$mapping['fieldName']] = $reflProp;
    }
    // change to !
    if ($this->reflClass->hasProperty($fieldName)) {
        $reflProp = $this->reflClass->getProperty($fieldName);
        $reflProp->setAccessible(true);
        $this->reflFields[$mapping['fieldName']] = $reflProp;
    }
    // ...
}

Convert Array of Id value to MongoId

Query by array of Id
$this->createQueryBuilder()->field('obj.id')->in( [ 'abcddc...', '123....' ] );

query that is pass to mongo is this.

db.document.find({"obj.$id": { "$in": ObjectId("abcddc...") }}

should be

db.document.find({"obj.$id": { "$in": [ObjectId("abcddc..."), ObjectId("123....")] }}

@PreUpdate & EmbedMany

We're seeing that a PreUpdate hook is causing the document not to update embedded documents without errors - even if array('safe' => true) is used on the flush.

An example:

/** @PreUpdate */
public function incrementUpdatedAt() {
    $this->updatedAt = new \DateTime();
}

Query results

Hello,

I make a query :
$users = $dm->getRepository('Documents\User')->findBy(array('username' => 'toto'));

if I make that :
while($users->hasNext())
{
$user = $users->getNext();
print_r($user);
}

The print_r shows me an array and not a User Document. Where is the problem ?
Please, help me.

Cannot modify data in preUpdate callback

From the documentation it can be extracted that if you wan to modify fields in the preUpdate event you need to call the UnitOfWork::recomputeSingleDocumentChangeSet() method, but calling this apparently it will always duplicate the record.

This is the relevant code:

    $document = $eventArgs->getDocument();
    $document->setUsername('other');
    $dm = $eventArgs->getDocumentManager();
    $uow = $dm->getUnitOfWork();
    $class = $dm->getClassMetaData(get_class($document));
    $uow->recomputeSingleDocumentChangeSet($class, $document);

After saving, I see two records, one with the old name and other with the new one

Performing an Upsert using QueryBuilder?

While doing an ->update() using the QueryBuilder, if I set the upsert to true using ->upsert(true), it does not work. I've to additionally also do a ->getQuery(array('upsert') => true) before I ->execute(). This behavior is unexpected and undocumented. It defeats the purpose of having the ->upsert(true) function.

$dm->createQueryBuilder('\Document\Foo')
   ->update()
   ->upsert(true)
   ->field('a')->equals('5783b417a6da7e6d77426428ab48e5f7')
   ->field('b')->equals('foo)
   ->field('count')->set(5)
   ->getQuery(array('upsert' => true))  // why is this required?
   ->execute();

Fix PHPDoc @return types in various places

In certain PHPDoc blocks for functions, the @return type is not of the right type. This causes auto-completion issues in various IDEs (I'm using PhpStorm).

For example, in \Doctrine\ODM\MongoDB\DocumentManager on line #341 the ->createQueryBuilder($documentName = null) function.

....
/**
Create a new Query instance for a class.
*
@param string $documentName The document class name.
@return Document\ODM\MongoDB\Query <--------------------------- MUST BE Query\Builder !
*/
public function createQueryBuilder($documentName = null) 
....

Similarly, there are such issues in other parts of the code. Is there a place where I can list them down as and when I find them and someone can fix it in the repository? (Unfortunately, I'm not yet a git user and due to time constraints, I can't fork and commit)

Problems with EmbedMany and discrimatorMap and discriminatorField

I'm experiencing a problem when trying to embed documents and specify my own discriminatorMap and Field.

This is the code I have:

/** @Document(collection = "ticket") */
class App_Model_Ticket extends App_Model_Abstract
{
    /**
     * @EmbedMany(
     *  discriminatorField = "type",
     *  discriminatorMap={
     *      "twitter"="App_Model_Ticket_Twitter",
 *      "email"="App_Model_Ticket_Email"
 *  })
 */
    protected $messages = array();

    // [...]
}


App_Model_Abstract is a MappedSuperClass.

App_Model_Ticket_Email and App_Model_Ticket_Twitter pretty much look alike right now:

/** @EmbeddedDocument */
class App_Model_Ticket_Email extends App_Model_Ticket_Message
{
}

and App_Model_Ticket_Message is another MappedSuperClass that extends App_Model_Abstract.

Now, whenever I create a new model and persist it to mongoDB, the entry gets created and looks like this:

[_id] => MongoId Object (
)
[messages] => Array (
    [0] => Array (
        [message] => Lorem ipsum
        [subject] => Ich bin ein Berliner22
        [dateSent] => MongoDate Object (
            [sec] => 1280909855
            [usec] => 0
        )
        [_doctrine_class_name] => App_Model_Ticket_Twitter
    )
    [1] => Array (
        [message] => Lorem ipsum
        [subject] => Ich bin ein Berliner22
        [dateSent] => MongoDate Object (
            [sec] => 1280909855
            [usec] => 0
        )
        [_doctrine_class_name] => App_Model_Ticket_Email
    )
)
[subject] => Ich bin ein Berliner22
[dateCreated] => MongoDate Object (
    [sec] => 1280909855
    [usec] => 0
)
[dateLastMessage] => MongoDate Object (
    [sec] => 1280909855

However, as you can see no type field for my embedded documents, it still uses _doctrine_class_name.

But when I try to fetch this object, I get an exception saying "Class not found".

I think this is because right now Doctrine ODM does not write into the custom discriminatorField with embedded documents, but reads from it.

Find and Modify (or Update) Documentation Incorrect

findAndModify() doesn't work as documented. From what I can tell, it should be findAndUpdate(), however, queries executed with that option return a bool (not the document). When I tried getSingleResult() instead of execute(), a 'query does not return iterator' exception was thrown.

class_parents error

Hello,

I can manipulate Mongo Documents.
But when I want to query (using find) some documents, with :
$users = $dm->getRepository('User')->findBy(array('name' => 'toto'));

I get this error :
class_parents(): Class User does not exist and could not be loaded in .../mongodb_odm/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php on line 336

Please, help me.

Undefined Index 'criteria' in DocumentPerister

On the master branch, a notice is thrown on line 531 of the DocumentPersister for an undefined index 'criteria'. This is when using the Yaml driver.

https://github.com/doctrine/mongodb-odm/blob/master/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php#L531

Full notice:
Notice: Undefined index: criteria in /Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php on line 531
PHP Warning: array_merge(): Argument #2 is not an array in /Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php on line 532

Warning: array_merge(): Argument #2 is not an array in /Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php on line 532
PHP Catchable fatal error: Argument 1 passed to Doctrine\MongoDB\Collection::find() must be an array, null given, called in Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php on line 533 and defined in Doctrine/MongoDB/Collection.php on line 169

Catchable fatal error: Argument 1 passed to Doctrine\MongoDB\Collection::find() must be an array, null given, called in Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php on line 533 and defined in /Doctrine/MongoDB/Collection.php on line 169

Illegal offset type in UnitOfWork.php on line 1334 when @Id(strategy="NONE")

I have a document with an @Id(strategy="NONE") for the $id field and if I set the value of this field to a type other than string, for example $document->setId(new \MongoBinData(md5(time())));. When I persist this document, I get the following warning

Warning: Illegal offset type in ...../Doctrine/ODM/MongoDB/UnitOfWork.php on line 1334

If I set the id to a string, then there's no such warning. Why is this happening? I want to have my _id field of a type other than MongoId (for example, I might want to store a binary hash here instead?)

DocumentRepository::findBy() Declaration

Fatal error: Declaration of Doctrine\ODM\MongoDB\DocumentRepository::findBy() must be compatible with that of Doctrine\Common\Persistence\ObjectRepository::findBy() in ../vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentRepository.php on line 39

see commit: doctrine/common@5a28553#diff-3

-    public function findBy(array $criteria);
+    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null);

Form > Upload file No mapping found for field 'file' in class 'Familio\CoreBundle\Document\Document'. (fixed)

$document = new Familio\CoreBundle\Document\Document; $formBuilder->add('file')->getForm();

Exception : No mapping found for field 'file' in class 'Familio\CoreBundle\Document\Document'.

I fixed this bug :

namespace Symfony\Bundle\DoctrineMongoDBBundle\Form;

use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\MongoDBException;

/**

  • Tries to guess form types according to ODM mappings
    *

  • @author Thibault Duplessis [email protected]
    /
    class DoctrineMongoDBTypeGuesser implements FormTypeGuesserInterface
    {
    /
    *

    • The Doctrine MongoDB document manager
    • @var DocumentManager
      */
      protected $documentManager = null;

    public function __construct(DocumentManager $documentManager)
    {
    $this->documentManager = $documentManager;
    }

    /**

    • @inheritDoc
      */
      public function guessType($class, $property)
      {
      if ($this->isMappedClass($class)) {
      $metadata = $this->documentManager->getClassMetadata($class);

      if ($metadata->hasAssociation($property)) {
          $multiple = $metadata->isCollectionValuedAssociation($property);
          $mapping = $metadata->getFieldMapping($property);
      
          return new TypeGuess(
              'document',
              array(
                  'document_manager' => $this->documentManager,
                  'class' => $mapping['targetDocument'],
                  'multiple' => $multiple,
                  'expanded' => $multiple
              ),
              Guess::HIGH_CONFIDENCE
          );
      } else {
          try {
              $fieldMapping = $metadata->getFieldMapping($property);
      
              switch ($fieldMapping['type'])
              {
                  case 'collection':
                      return new TypeGuess(
                          'Collection',
                          array(),
                          Guess::MEDIUM_CONFIDENCE
                      );
                  case 'boolean':
                      return new TypeGuess(
                          'checkbox',
                          array(),
                          Guess::HIGH_CONFIDENCE
                      );
                  case 'date':
                  case 'timestamp':
                      return new TypeGuess(
                          'datetime',
                          array(),
                         Guess::HIGH_CONFIDENCE
                      );
                  case 'float':
                      return new TypeGuess(
                          'number',
                          array(),
                          Guess::MEDIUM_CONFIDENCE
                      );
                  case 'int':
                      return new TypeGuess(
                          'integer',
                          array(),
                          Guess::MEDIUM_CONFIDENCE
                      );
                  case 'string':
                      return new TypeGuess(
                          'text',
                          array(),
                          Guess::MEDIUM_CONFIDENCE
                      );
              }
          } catch (MongoDBException $e) {
              // not an entity or mapped super class
          }
      }
      

      }

      return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
      }

    /**

    • @inheritDoc
      */
      public function guessRequired($class, $property)
      {
      if ($this->isMappedClass($class)) {
      $metadata = $this->documentManager->getClassMetadata($class);

      if ($metadata->hasField($property)) {
          if (!$metadata->isNullable($property)) {
              return new ValueGuess(
                  true,
                  Guess::HIGH_CONFIDENCE
              );
          }
      
          return new ValueGuess(
              false,
              Guess::MEDIUM_CONFIDENCE
          );
      }
      

      }
      }

    /**

    • @inheritDoc
      */
      public function guessMaxLength($class, $property)
      {
      if ($this->isMappedClass($class)) {
      $metadata = $this->documentManager->getClassMetadata($class);

      if (!$metadata->hasAssociation($property)) {
      
              $mapping = $metadata->getFieldMapping($property);
      
      
              if (isset($mapping['length'])) {
                  return new ValueGuess(
                      $mapping['length'],
                      Guess::HIGH_CONFIDENCE
                  );
              }
      
      }
      

      }
      }

    public function guessMinLength($class, $property)
    {
    }

    /**

    • Returns whether Doctrine 2 metadata exists for that class
      *
    • @param string $class
    • @return Boolean
      */
      protected function isMappedClass($class)
      {
      return !$this->documentManager->getConfiguration()->getMetadataDriverImpl()->isTransient($class);
      }
      }

Doctrine stores ObjectIds, but returns them as strings

Doctrine seems to correctly store ObjectId's. However, when retrieving them back out of the database doctrine returns plain strings. This is a problem when using these ObjectIds in queries because if you query on an ObjectId and pass in a string it will not match and you'll get no result.

I know you can just do a new \MongoId(ObjectIdString) but what about when you're storing an array of ObjectId's? For instance an array of ObjectIds a user is following. Then in ->getFollowing() you must loop through each string and create a MongoId before returning the array of following ObjectIds. This is a practical case that I'm running into right now while trying to create a whereIn query. I need ObjectIds to do it, not strings.

Thoughts?

Class *** is not a valid document or mapped super class.

Hello,

we are using the current versions of

doctrine-mongodb
doctrine-mongodb-odm
DoctrineMongoDBBundle

installed on the latest symfony2-standard package.

Our server is running the 1.4.4 version of mongoDB-server.

The odm is configured using annotations.

The second request to a page, which relies on database-request throws following exception:

Doctrine\ODM\MongoDB\MongoDBException: Class _\DataBundle\Document\Form is not a valid document or mapped super class. (uncaught exception) at /home/_/project/symfony/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/MongoDBException.php line 141

After clearing the cache, the first request is successful, the next fails again.

We have tried different workarounds:

  • updated vendors to the most recent versions
  • using yml configuration instead of annotations
  • using manual mapping instead of auto_mapping

None of them worked.

Which PHP version is right for mongodb-odm?

On developers machines we have PHP 5.3.5 and everything works fine. But on test server we have 5.3.2 and we have a lot of problems.

So, is 5.3.2 is not so good for ODM?

If so, maybe you can write notice somewhere what better to use 5.3.5 and latest versions?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.