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

How to implement database the table like this "prefix_tablename" ik_user, ik_blog; Table Prefix ?

My tables are like these ik_user, ik_config, ik_blog,

How to auto add the prefix 'ik_' to models ???

//user.php Models

namespace IKPHP\Common\Models;
class User extends BaseModel
{
    public $userid;

    public function getSource()
    {
        return $this->tb_prefix."user"; //How to implement like this "prefix_tablename"  ik_user
    }
    public function afterSave()
    {
        if ($this->id) {
            //清缓存
            $viewCache = $this->getDI()->getViewCache();
            $viewCache->delete('post-' . $this->id);
        }
    }    
}
//config.php    'prefix'   => 'ik_'
//////////////////////////////////////////////////////
return new \Phalcon\Config(

'database' => array(
    'database' => array(
    'adapter'  => 'mysql',
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'name'     => 'ikphp',
    'prefix'   => 'ik_', // table prefix
),

));
//////////////////////////////////////////////////////
// service.php

$di->set('db', function() use ($config) {

    $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => $config->database->host,
        "username" => $config->database->username,
        "password" => $config->database->password,
        "dbname" => $config->database->name,
        "prefix" => $config->database->prefix,
        "options" => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        )
    ));

    if ($config->application->debug) {
        //开启日志记录
        $eventsManager = new Phalcon\Events\Manager();
        $logger = new \Phalcon\Logger\Adapter\File($config->logger->path .date('Y-m-d').'db.log');
        //监听数据库日志
        $eventsManager->attach('db', function($event, $connection) use ($logger) {
            if ($event->getType() == 'beforeQuery') {
                $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
            }
        }); 
        $connection->setEventsManager($eventsManager);      
    }

    return $connection;
});


7.0k
Accepted
answer

I found This. I think it may help you ;)



13.1k
edited Jul '14

I want the table prefix in a configuration for Phalcon Model



13.1k
edited Jul '14

my configuration prefix; how to use

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 config.php

//config.php    'prefix'   => 'ik_'
//////////////////////////////////////////////////////
return new \Phalcon\Config(
'database' => array(
    'database' => array(
    'adapter'  => 'mysql',
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'name'     => 'ikphp',
    'prefix'   => 'ik_', // table prefix
),
));

I think you may configure a

BaseMode.php setting the prefix,

class BaseModel extends Phalcon\Mvc\Model{
    public function getSource()
    {
        return 'my_'.strtolower(get_class($this));
    }
}

and all your models must extend BaseModel.php, like that all your models will be set as you wish.

class Users extends BaseModel
{

}

class Blog extends BaseModel
{

}

I don't think so you can directly configure it in the config file, but maybe i'm wrong



13.1k
edited Jul '14
<?php
namespace IKPHP\Common\Models;

class BaseModel extends \Phalcon\Mvc\Model
{
    public $tb_prefix;

    public function initialize()
    {
        $this->tb_prefix = $this->getDI()->get('config')->database->prefix;//  ik_blog
    }   
}

configure prefix in the config file like this : this success run;

 return array(
    'adapter'  => 'mysql',
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'name'     => 'ikphp',
    'prefix'   => 'ik_',
);

I think, this is the way to success



13.1k
edited Jul '14
<?php
namespace IKPHP\Common\Models;

class User extends BaseModel
{
    public $userid;

    public function getSource()
    {
        return $this->tb_prefix."blog";
    } 
}