0

I need some help in Doctrine ODM, in some cases the hydrate do not return the expected values. I have on project as example and when I query for a collection and all work fine with hydrate, this is the case that works:

ProductsCollection:

<?php
namespace MongodbManager\Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\Common\Collections\ArrayCollection;
/** @ODM\Document */
class ProductsODM
{
    /** @ODM\Id */
    public $id;
    /** @ODM\Field(type="string") */
    public $name;
   /** @ODM\Field(type="float") */
   public $price;
   /** @ODM\Field(type="date") */
   public $date;
   /** @ODM\EmbedMany(targetDocument=CategoriesODM::class) */
   public $categories;
   /** @ODM\ReferenceMany(targetDocument=TagsODM::class, storeAs="id") */
   public $tags;
   /** @ODM\ReferenceOne(targetDocument=TagsODM::class, storeAs="id") */
   public $tagsPrimary;
   public function __construct()
   {
       $this->categories = new ArrayCollection();
   }
}

TagsCollection

 <?php
 namespace MongodbManager\Documents;
 use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
 /** @ODM\Document */
class TagsODM
{
    /** @ODM\Id */
    public $id;
    /** @ODM\Field(type="string") */
    public $name;
}

Query (WORKS!)

$this->dm->createQueryBuilder(ProductsODM::class)->hydrate(true)->getQuery()->execute();

Return (content):

{
        "id": "5d9e23b32b251b2fc7438d14",
        "name": "Martini 1570644915",
        "price": 5.99,
        "date": {
            "date": "2019-10-09 15:15:15.000000",
            "timezone_type": 3,
            "timezone": "America/Sao_Paulo"
        },
        "categories": [
            {
                "id": "5d9e23b32b251b2fc7438d15",
                "name": "bar"
            },
            {
                "id": "5d9e23b32b251b2fc7438d16",
                "name": "night"
            }
        ],
        "tags": [
            {
                "id": "5d9dde11982e830950453618",
                "name": "COMIDA"
            },
            {
                "id": "5d9dde11982e830950453619",
                "name": "BEBIDA"
            }
        ],
        "tagsPrimary": {
            "id": "5d9dde11982e830950453618",
            "name": "COMIDA"
        }
    }

As you can see the result came with Tags values.

HERE IS THE PROBLEM:in the UsersODM when I query for documents do not include the values of UsersGrousODM in the result, just the id, here is the code:

<?php
namespace MongodbManager\Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** @ODM\Document */
class UsersODM
{
    /** @ODM\Id */
    public $id;
    /** @ODM\Field(type="string") */
    public $users_login_code;
    /** @ODM\Field(type="string") */
    public $users_username;
    /** @ODM\Field(type="string") */
    public $users_password;
    /** @ODM\Field(type="string") */
    public $users_status;
    /** @ODM\Field(type="date") */
    public $users_update_date;
    /** @ODM\ReferenceOne(targetDocument=UsersGroupODM::class, storeAs="id") **/
    public $users_group_id;
}

The UsersGroupODM

<?php
namespace MongodbManager\Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** @ODM\Document */
class UsersGroupODM
{
    /** @ODM\Id */
    public $id;
    /** @ODM\Field(type="string") */
    public $users_group_name;
    /** @ODM\Field(type="string") */
    public $users_group_status;
}

The Query: $this->dm->createQueryBuilder(UsersODM::class)hydrate(true)->getQuery()->execute();

Return:

{
    "id": "5d9e2b0991055769a44078c6",
    "users_login_code": null,
    "users_username": "webmaster",
    "users_password": "d68b87ecdf82164583816f1306f4c342ba57ad3e......",
    "users_status": "active",
    "users_update_date": {
        "date": "2019-10-09 15:46:33.000000",
        "timezone_type": 3,
        "timezone": "America/Sao_Paulo"
    },
    "users_group_id": {
        "id": "5d9e289358063a6594142cbc"
    },
    "users_info": null
}

the users_group_id do not include the field users_group_name and users_group_status.

  • `users_group_id` is a reference so ID is the only thing that is in DB. What exactly are you dumping? Given you're using `->hydrate(true)` I wouldn't expect a plain JSON dumped – malarzm Oct 10 '19 at 15:07
  • @malarzm as you can see, in the first example, tags and tagsPrimary also are stored as ObjectId in mongodb, but when I query it return with the values of the documents tagsODM – Rafael Guimarães Oct 10 '19 at 15:18
  • The question is how are you serializing data from the database. For instance a group in the User document being dumped might be a proxy and if it's not initialized then only id field is populated, the rest awaits for interaction. In First example it might not be the case as dumping tags collection has loaded all documents and then primary tag has data already in place. – malarzm Oct 10 '19 at 18:58
  • @malarzm, `For instance a group in the User document being dumped might be a proxy and if it's not initialized` how to initialize? the proxy is create by doctrine. `In First example it might not be the case as dumping tags collection has loaded all documents and then primary tag has data already in place.` this do not make sense, as you can see the two examples are identical and the second do not came with values. – Rafael Guimarães Oct 10 '19 at 21:07
  • It does make sense, everything depends on how are you serializing data to JSON. The proxy can be initialized either by an explicit call to `__load()` method or by accessing any method/property. – malarzm Oct 11 '19 at 08:32
  • @malarzm, I show in example how I am querying, still dont get how it work on first and not in second example. But thanks for trying. – Rafael Guimarães Oct 11 '19 at 17:56
  • Everything looks fine for me, so I have only two idea. One, you might have to clear doctrine cache/rebuild proxies. Another, is that in those documents of `UsersGroupODM` you simply have only ids in your MongoDb, so doctrine is not picking up nonexistent fields. – yergo Apr 03 '20 at 21:02

0 Answers0