Tag Archives: config

CakePHP3 separate config file for each environment


I’ll write how to separate config file in CakePHP3 version 3.0.9. As for FuelPHP, you can add setting file into app/config/development, etc.

In the case when creating setting file in CakePHP3 for each environment, I’ll recommend you to create the directory config/development and put setting files in it, and modify configure loading code.

Modified Code

Add one line with Configure::load into config/bootstrap.php. In the following code, it’s forth line. Suppoted that $environment is set. You should define it somewhere.

At the forth line, it will merge setting file config/{$development}/app.php into config/app.php.

You can add any other conditional statement like if or switch.

Explanation

What does Configure do?

According to bootstrap.php, Configure::config and Configure::load seems to concern loading setting file. Let’s look at these functions.

Class Configure is defined at vendor/cakephp/cakephp/src/Core/Configure.php.

Configure::config

According to the comment, this method adds an engine. First parameter is the key to identify added engines, seconed one is the instance of the engine. You know, if you set the engine with the same name twice or more times, the setting is overwritten by last one.

bootstrap.php adds engine new PhpConfig() as default one, with Configure::config('default', new PhpConfig());.

Configure::load

The method loads setting with the engine set with Configure::config.

First parameter specify the file to be load. When you use /, it means directory separator.

Second parameter specify the engine to handle settings, which is set with Configure::config. The identify key of the engine is expressed as $name in Configure::config, but it’s $config in here. On specifying the engine unset, return false.

Third parameter defines to merge or to overwrite. I’ll set it true, because common setting in all environments should be written in one file.

So, you can merge setting file with Configure::load(filepath, 'default', true). You know I didn’t explain the detail like Hash::merge.

If load not existing file?

You can know what will occur when you try it. But let’s look at what procedure works. According to bootstrap.php and Configure::load, the setting file will be load with read method in PhpConfig. PhpConfig is defined at vendor/cakephp/cakephp/src/Core/Configure.php.

It says error will be occurred. The variable $config seems a relic on the past


Database available in FuelPHP


I wondered what database is available in FuelPHP, and look into FuelPHP code.

Environment

  • FuelPHP 1.8 (GitHub)

Available Database

The following DBMS is theoretically available in FuelPHP. It means that FuelPHP use driver for the following databases.

  • Cubrid
  • FreeTDS
  • Microsoft SQL Server
  • Sybase
  • Firebird
  • IBM DB2
  • IBM Informix Dynamic Server
  • MySQL
  • Oracle Call Interface
  • ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
  • PostgreSQL
  • SQLite 3, 2
  • SQL Azure
  • 4D

How to investigate

First, read manual. At the section Database Introduction, it says Can be mysql, mysqli or pdo. Namely, FuelPHP can handle mysql specific driver and PDO driver.

Then, read PDO section of PHP document. For above all DBMS, PDO is available.

Well, can FuelPHP handle PDO and connect to DB in real? Now, look at the FuelPHP core code, which handle database connection. There are 3 kind of connection.php, each of them handle mysql, mysqli, PDO connection. It’ll be clear PDO is available in FuelPHP.

Take your Attention when you use sqlite

You should be careful when you use sqlite. (FuelPHP version 1.7.2)

Primary Key

Migration doesn’t work when you add Primary Key like MySQL. In sqlite3, we can’t add name to Primary Key. 例えば MySQL なら Primary Key id (id) となるところが、 sqlite3 だと Primary Key (id) というようになります。

So, we should create plain SQL or change FuelPHP core code. It’s a choice not to use Primary Key, but I don’t recommend.

また通常は migration file を php oil を使って作成すると 自動で UNSIGNEDid が作成されて AUTO_INCREMENT が設定されますが、 sqlite ではこれが動きません。 sqlite では AUTO_INCREMENT ではなく AUTOINCREMENT になります。 いまのところ自動生成のスクリプトでは対応していませんので FuelPHP の core のコードを変更する必要があります。 そして自動生成される UNSIGNED は 削除しましょう。 sqlite では UNSIGNED BIT INT というのが使えますが、 使ったところで INTEGER と解釈されます。

Blank Charset

sqlite3 では charset の指定方法が他のデータベースとは異なります。 PRAGMA statement を使います。 (確か sqlite3 では標準文字コードが utf-8 だったと思います。)

If you don’t set charset in db config (config/db.php), php oil migrate occurs an error of SQL, CREATE TABLE, because SQL which contains DEFAULT CHARACTER SET utf8 at last is executed. To prevent that, set empty string to charset, charset => ''. すると、 DEFAULT から始まる文字コード指定部分がなくなり、 sqlite3 でも php oil migrate が動くようになります。

db.php は次のように書きます。

このようにすると、 環境ごとに別の DB が作成されます。 table_prefixphp oil r migration の時には不要ですが、 php oil r migration:down の時に必要になることがあります。

Take your Attention when you use PostgreSQL

Migration doesn’t work. It is because PostgreSQL SQL syntax is somehow different from MySQL one. I recommend you to use FuelPHP Plugin for PostgreSQL.