Configuring a DB connection
Yii2 offers a high-level layer to access databases, built on top of PHP Data Objects (PDO).
This framework allows us to manipulate a database table's content through the use of ActiveRecord objects. This encapsulates methods to access single or multiple records, as well as filtering, joining, and ordering data in an intuitive way.
Again, we can work with databases using plain SQL, but this means that we must handle dissimilarities in SQL languages passing through different databases (MySQL, SQL Server, Postgres, Oracle, and so on), which means losing Yii2 facilities.
A database object connection is an instance of yii\db\Connection
:
$db = new yii\db\Connection([ 'dsn' => 'mysql:host=localhost;dbname=my_database', 'username' => 'my_username', 'password' => 'my_password', 'charset' => 'utf8', ]);
In this example, we have a connection to a MySQL Server with...