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

I would like to set up a table prefix for all my models, How can set config.php

Regarding my configuration prefix; how to use it?

I would like to set up a table prefix for all my models, since this is how it is designed in the database.

  • How can I achieve that?
  • How can set it in config.php
  • How can I use in Model
class Users extends Phalcon\Mvc\Model
{
    public function getSource()
    {
        //return 'ik_' . 'users';
        return $this->get('config')->database->prefix.'users';
    }
}
//config.php    'prefix'   => 'ik_'
//////////////////////////////////////////////////////
return new \Phalcon\Config(
    'database' => array(
        'adapter'  => 'mysql',
        'host'     => 'localhost',
         'username' => 'root',
        'password' => '',
        'name'     => 'ikphp',
        'prefix'   => 'ik_', // table prefix
),
));


98.9k
Accepted
answer

Set the configuration in the DI:

$di->set('config', $config);

This is the config: https://github.com/phalcon/forum/blob/master/app/config/config.example.php

And this is where the config is set as service: https://github.com/phalcon/forum/blob/master/app/config/services.php#L200

This way you can access the configuration in the models.



13.1k
edited Jul '14

Thank you