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 make the database connection to be utf-8 compliant in this setting ?

This is my index.php file ( the database is 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();
}
?>

How to make the database setting to be utf-8 compliant in this case ?


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

https://forum.phalcon.io/discussion/10/mysql-utf8-encoding

in config.php

return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'root',
        'password'    => '',
        'dbname'      => 'my_db',
        'charset'       => 'utf8',

    )
));

in services.php

$di->set('db', function () use ($config) {
    return new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        'charset' => $config->database->charset
    ));
});