Configuration - Getting Started
This section assumes you have already set up your project and have already gone through the Api Tutorial guide atleast.
Introduction
Pionia uses PORM to interact with the database. Porm is a simple and lightweight QueryBuilder that is built on top of the medoo framework. Porm provides a set of tools and conventions that make it easy to interact with the database in PHP. PORM is designed to be simple, lightweight, and easy to use.
Installation
If you want to check out PORM alone or want to use it outside the Pionia framework, you can install it via composer.
If you are using Pionia, you do not need to install PORM separately. PORM is already included in the Pionia framework.
Configuration
Configuring PORM is simple. All you need is the settings.ini file in the root of your project. The settings.ini file should contain the following:
Supported Databases
- Postgres (PostgreSQL) via the
pgsql
driver - MySQL/MariaDB via the
mysql
driver - Oracle via the
oci
driver - Sybase via the
dblib
driver - MSSQL via the
sqlsrv
ordblib
driver - SQLite via the
sqlite
driver
Please remember to install the necessary PHP extensions for the database you are using in order to connect to the database. This usually happens in the php.ini
file.
If you are using Pionia, you do not need to configure PORM separately. PORM is already configured in the Pionia framework.
Multiple Database Connections
If you want to connect to multiple databases, you can do so by adding the database connection settings to the settings.ini
file. You can then specify the database connection to use when querying the database.
You can then specify the database connection to use when querying the database.
By default, PORM will use the default database connection(db) to query the database.
Accessing the Underlying Medoo Instance
If you need to access the underlying Medoo instance, you can do so by calling the getDatabase
method on the Porm class.
The $database
variable will contain the Medoo instance, which you can use to interact with the database directly availing every method medoo provides.
Getting the last inserted ID
To get the last inserted ID after inserting a record into the database, you can call the lastId
method on the Porm class.
The $lastId
variable will contain the last inserted ID.
Usage
PORM does not rely on models to interact with the database. Instead, you get to interact with the database directly. This comes with a lot of flexibility and simplicity. Porm also interacts with both new and existing databases.
All Queries originate from the Porm
instance. Here is an example of how to interact with the database using PORM.
Determining the target table
All queries start by determining the target table. This is done by calling the from
method on the Porm
instance. The from
method takes the table name as the first argument. The table name must match the table name in the database.
You can also alias the table name by passing the alias as the second argument.
As of v1.0.2
, You can achieve the above using the table
method. This method is exactly the same as the from
method however it is more readable.
You can also define the connection to use at this point.
Defining the columns
To select specific columns from the table, you can use the columns
method. This method should be called on the Porm
instance.
This can used whether getting one or multiple records.