如何在此设置中使数据库连接符合utf-8标准?

时间:2015-08-29 13:29:09

标签: phalcon

这是我的index.php文件(数据库是MySQL):

<?php

use Phalcon\Loader;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

include("../app/config_const.php");
include("../app/config_inc.php");
include("../app/lib/PSR.php");
try {

    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/'
    ))->register();

    // Create a DI
    $di = new FactoryDefault();

    // Setup the database service
    $di->set('db', function(){
        $cnx = new DbAdapter(array(
            "host"     => "localhost",
            "username" => "root",
            "password" => "",
            "dbname"   => BDD
        ));
        return $cnx;
    });

    // Setup the view component
    $di->set('view', function(){
        $view = new View();
        $view->setViewsDir('../app/views/');

        $view->registerEngines(array(
            ".phtml" => 'Phalcon\Mvc\View\Engine\Volt'
        ));
        return $view;
    });

    // Setup a base URI so that all generated URIs include the "phalcon" folder
    $di->set('url', function(){
        $url = new UrlProvider();
        $url->setBaseUri('/resto/');
        return $url;
    });

    //Register the flash service with custom CSS classes
    $di->set('flash', function(){
        $flash = new \Phalcon\Flash\Direct(array(
            'error' => 'alert alert-error',
            'success' => 'alert alert-success',
            'notice' => 'alert alert-info',
        ));
        return $flash;
    });

    //Handle the request
    $app = new Application($di);

   echo $app->handle()->getContent();

} catch(\Exception $e) {
     echo "Exception:  ", $e->getMessage();
}
?>

在这种情况下如何使数据库设置符合utf-8

2 个答案:

答案 0 :(得分:3)

将编码参数添加到DB适配器:

// Setup the database service
$di->set('db', function(){
    $cnx = new DbAdapter(array(
        "host"     => "localhost",
        "username" => "root",
        "password" => "",
        "dbname"   => BDD,
        "charset"  => "utf8",
        "options" => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        ),
    ));
    return $cnx;
});

Reference

您也可以将编码设置为.htaccess文件:

# your file could start with something like this
RewriteEngine On
RewriteBase /
# add next line
AddDefaultCharset utf-8

答案 1 :(得分:0)

好的我找到了解决方案:我修改了ssp.class.php以进行服务器处理:

 private void Log(string message, bool debugOnly, bool waitOnThis, LogType logType, string culturename, int langid, int themeid, int saleschannel)

        {
            string logFormat = Environment.NewLine + "{0}\t  {1}\t{2}" + Environment.NewLine;
            //message = message + " - " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff");
            message = string.Format(logFormat, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),logType.ToString().Substring(0,3).ToUpper(), message);
            _logFileName = string.Format(_logFileNamePattren, _jobConfigurationData.Name, LogFileId, _logFileDate);
            string filePath = _jobConfigurationData.LogFolderPath;
            if (!string.IsNullOrEmpty(filePath))
                filePath += "\\" + _logFileName;
            if (debugOnly == false || (debugOnly && _jobConfigurationData.BuildMode == BuildMode.Debug))
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    if(!Directory.Exists(Path.GetDirectoryName(filePath)))
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    File.AppendAllText(filePath, message, System.Text.Encoding.UTF8);
                }
                Console.Write(message);
            }