Configuration
Database connect setting
The connection is set in htdocs/_fegg/site/config/database_config.php. The skeleton's file reads these values with feggEnv() (an environment variable, then site/config/env.php, see Settings), so the file itself is committed and the same on every server.
| Name | Default | Remarks |
|---|---|---|
FEGG_DB_DSN | sqlite: + FEGG_DB_PATH | PDO DSN |
FEGG_DB_USER | '' | User name (MySQL) |
FEGG_DB_PASSWORD | '' | Password (MySQL) |
FEGG_DB_SLAVE_DSN | FEGG_DB_DSN | Read-only replica |
FEGG_DB_PATH | site/data/db/db.db | SQLite file. As an environment variable without FEGG_DB_DSN, it wins over FEGG_DB_DSN in env.php (the test suite uses it) |
The file fills two entries. dsn, username and password are passed to PHP's PDO:
$database_config['master'] = array('dsn' => ..., 'username' => ..., 'password' => ...);
$database_config['slave'] = array(
array('dsn' => ..., 'username' => ..., 'password' => ...), // more than one server allowed
);
masteris used for INSERT, UPDATE, DELETE, schema changes and transactions. SELECT and PRAGMA queries use one of theslaveentries, picked at random. With a single database, both use the same DSN.new \Fegg\Database\Pdo('name')readsname/database_config.phpandname/database_regular_use_query.phpinstead, for a second database.
MySQL
SQLite is the default. For MySQL (5.7 / 8.x) or MariaDB, set the connection in site/config/env.php (or as environment variables):
'FEGG_DB_DSN' => 'mysql:host=127.0.0.1;port=3306;dbname=example;charset=utf8mb4',
'FEGG_DB_USER' => 'example',
'FEGG_DB_PASSWORD' => '...',
- Always add
charset=utf8mb4to the DSN, or emoji and other 4-byte characters are lost. - The connection runs with
sql_mode = ANSI_QUOTES: Fegg quotes names with", so in raw SQL write string literals with single quotes (WHERE title = 'foo', not"foo", which is a column name). Queries are buffered. - Outside production the connection is strict (
FEGG_ENVdevelopment or staging, see Settings): a value the column cannot hold throwsQueryException, such as''or text in anINTcolumn (Incorrect integer value: '' for column 'image_id'), more than 255 characters in aCHAR, or an impossible date. In production the mode stays lenient, and MySQL stores0, a cut text or a zero date without an error: such mistakes show on the develop server instead of losing data on the live site. Give the number and date columns a form can leave out a bind type (see addBindParam). ./fegg-cli migration,migration:db2schemaand/cms/migrate/work on MySQL as on SQLite. Tables are created as InnoDB withutf8mb4_unicode_ci, and changed tables are altered in place (ADD/MODIFY/DROP COLUMN).
The YAML types are mapped like this (a size written in the YAML, such as VARCHAR(100), is used as written):
| YAML type | MySQL type |
|---|---|
INTEGER | INT |
CHAR | VARCHAR(255) |
TEXT | MEDIUMTEXT |
BOOLEAN | TINYINT(1) |
DATETIME | DATETIME |
BLOB | LONGBLOB |
REAL | DOUBLE |
The YAML type is kept in the column comment (fegg:CHAR), so migration:db2schema writes the same YAML back. Indexes on TEXT columns use the first 191 characters.
fegg-cli migration drops the tables and columns that are not in the YAML files (see migration): give the site a database of its own, never one shared with another application.
The skeleton's MySQL tests run with composer test:mysql against the server set in FEGG_TEST_MYSQL_DSN, FEGG_TEST_MYSQL_USER and FEGG_TEST_MYSQL_PASSWORD. Each test creates and drops its own database.
Automatic append query configuration
The queries added automatically to queries built by Fegg\Model and the build methods of Fegg\Database\Pdo are set in htdocs/_fegg/core/config/database_regular_use_query.php. To change them, copy the file to site/config/ (see Overriding the core); the whole file is replaced.
CURRENT_TABLE is replaced with the table name, and now() with the current date and time. An item that the query already sets (for example valid given to insertRow()) is not added again.
<?php
defined('FEGG_CORE_DIR') || exit;
/**
* Automatically appended queries
*
* Queries appended automatically when the "select", "insert", "update" and "delete" methods of the DB class run
* (to be extended as needed).
*/
$database_regular_use_query = array(
// Queries appended regardless of the table
'regular_use' => array(
'select' => array(
'where' => 'CURRENT_TABLE.valid = 1',
'order' => '',
),
'count' => array(
'where' => 'CURRENT_TABLE.valid = 1',
'order' => '',
),
'insert' => array(
'item' => 'modified = now(), created = now(), valid = 1',
),
'update' => array(
'item' => "modified = now()",
'where' => '',
),
),
/*
// Queries appended per table
'table' => array(
'table_name' => array(
'select' => array(
'where' => 'show_flag = 0',
'order' => '',
),
'insert' => array(
'item' => 'update_time = now(), create_datetime = now()',
),
'update' => array(
'item' => 'update_time = now()',
'where' => '',
),
),
),
*/
);
These queries are why the model needs its required columns. To skip these queries, see unsetRegularUseQuery(), unsetRegularUseQueryForTable() and disabledRegularUseQuery() (Model, Pdo).