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

Creating a new MongoDB collection dynamically and accessing a model with a dynamic collection

Hi to All,

I have been creating an app that utilizes Phalcon and MongoDB. The tricky part is that, I can't find any lead that allows Phalcon to dynamically create a collection. Or if there's any, I might have missed it. Is there anyone that could help me point out how to do it with Phalcon? Or if it is not available, does Phalcon have any plans on adding this feature or was there any issues that prohibits it from being developed?

Also, is the changing of collection reference in a model using destruct and re-initialisation works with the latest version of Phalcon or do i have to downgrade to a specific version of Phalcon to do something like this?

Sorry for asking a lot of questions but I happen to start to love Phalcon because of its performance and I would like to learn more about its capabilities, and whats not.

Thanks a lot,

Jay



9.5k

Fixed creating a new MongoDB collection dynamically. For those interested, you can issue a command via MongoDB execute method. Just figuring out on dynamically setting collection in a model. If you have your two cents, let me know, that would be great. Thanks.



9.5k
Accepted
answer

Fixed using either command() or execute() under MongoDB, which is also inherited by MongoClient. Hope this would be useful for others as well.

edited Jan '17

hiii, I'm new with phalcon. how about make some adapter like this :

class AdapterModels
{
    protected $table;
    protected $models;
    public function __construct($table)
    {
        $this->table = $table;
        $GLOBALS['ModelName'] = $this->table;
        $this->models   = new BasicModels();
    }

    public function init(){
        return $this->models;
    }

    public static function __callStatic($name, $arguments)
    {
        $GLOBALS['ModelName'] = $arguments[0];
        $models   = BasicModels::$name($arguments[1]);
        return $models;
    }
}

and add some tricky on model like this :

public function getSource() {
    global $ModelName;
    return $ModelName;
}

and we can call it like this :

$student    = AdapterModels::find('student',[
    'conditions'=>$conditions,
    'sort'  =>$order,
    'limit' =>$limit,
    'skip'  =>$skip
]);

and : $adapter = new AdapterModels($this->table_name); $modules = $adapter->init();