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.



18.5k

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).



12.6k

Your comment is not clear for me.



18.5k
edited Jul '14

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



83.4k

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

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


12.6k
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.



83.4k
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();