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

Avoiding dummy models for each table

To use query builder I have to create a class for each table: class Foo extends Phalcon\Mvc\Model{}

Is there any way to avoid this?

I think query builder should create non-predifiend models in background and implicity.



33.8k

I don't think so, 'cause you need to store the result of executeQuery in a instance of the Model you search in the db (at least from what I know and read).



22.1k

Your comment is not clear for me.



33.8k
edited Jul '14

I mean that you'll need a variable of some type (model) in which the data resultset can be saved.



98.9k

You can create an autoloader that automatically defines not existing classes:

spl_autoload_register(function($className) {
  eval("class $className extends \Phalcon\Mvc\Model { };");
});


22.1k
edited Jul '14

I think adding such autoloader is not safe. I need only create class when the class is refered by the query builder, not for all unavailable classes.



98.9k
edited Jul '14

Using a namespace you can filter those classes that are models:

spl_autoload_register(function($className) {
    if (strpos($className, 'App\Models') !== false) {
        eval("class $className extends \Phalcon\Mvc\Model { };");
    }   
});
$robots = $this->modelsManager->createBuilder()
    ->from('App\Models\Robots')
    ->getQuery()
    ->execute();