colinmollenhour / mongodb-php-odm

A simple but powerful set of wrappers for using MongoDb in PHP (also a Kohana 3 module)
BSD 3-Clause "New" or "Revised" License
208 stars 50 forks source link

A bit magic Mongo_Collection::collection #65

Closed sergeyklay closed 1 year ago

sergeyklay commented 10 years ago

In current master branch PHPDoc of Mongo_Collection::collection says:

 /**
  * Get the corresponding MongoCollection instance
  *
  * @return MongoCollection
  */

But this is wrong. Let's look at it again:

public function collection()
{
    $name = "$this->db.$this->name.$this->gridFS";
    if( ! isset(self::$collections[$name]))
    {
        $selectMethod = ($this->gridFS ? 'getGridFS' : 'selectCollection');
        self::$collections[$name] = $this->db()->db()->$selectMethod($this->name);
    }

    return self::$collections[$name];
}

Note that the $this->db()->db() returns the \MongoDb instance directly. In this case:

  1. $this->db()->db()->gridFS($this->name); Return \MongoGridFS
  2. $this->db()->db()->selectCollection($this->name); Return \MongoCollection
  3. For gridFS need a prefix (by default 'fs'), rather than the collection name

It seems Mongo_Collection::collection method returns not what was expected from him. At least in some cases getGidFS started with the wrong params.

Also note: in some cases:

$name = "$this->db.$this->name.$this->gridFS";

is a

$name = null + null + false;

Best regards : )

colinmollenhour commented 10 years ago

I think I actually originally intended collection to be able to return the gridfs instance and in this case the collection name is actually the gridfs prefix. However, I admit that the gridfs functionality is only half-baked and I don't think I ever used it personally..