We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

MogoDB ODM Incubator Problem

I'm having a problem with MogoDB ODM Incubator.

When a Model class was already used to find records it uses the source collection correctly. But if I use the class again, then the source changes to [model name]_model.

Like this:

$country = CountriesModel::findFirst([["name"=>['$eq'=>"country name"]]]);

In the MongoDB profiler I see this:

{
    "op" : "query",
    "ns" : "entregueme.countries",
    "query" : {
        "find" : "countries",
        "filter" : {
            "name" : {
                "$eq" : "Brasil"
            }
        }
    },

Which is right, because the source is countries for that model.

But on the second time it changes the model source to:

$country = CountriesModel::findFirst([["name"=>['$eq'=>"country name"]]]);

And then the MongoDB profiler I see this:

{
    "op" : "query",
    "ns" : "entregueme.countries_model",
    "query" : {
        "find" : "countries_model",
        "filter" : {
            "name" : {
                "$eq" : "Brasil"
            }
        }
    },

So, it somehow changes the model source to something close to the model class name.

Anyone knows why?

Solution:

  1. Name your collections all lower case.
  2. Name your models just with the first character uppercase and equal to the collection name.

Clearly, it's a bug in the incubator.

Solved:

My models where like this:

class CitiesModel extends MongoCollection {
    public function initialize()
    {
        $this->setSource('cities');
    }
}

But need to be like this:

class CitiesModel extends MongoCollection {
    public function initialize()
    {
        $this->setSource('cities');
    }
    public function getSource()
    {
        return 'cities';
    }
}

Because MongoCollection class always call getSource() like this in MongoCollection line 235.

$source = $collection->getSource();