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;
});