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 (Pionia ORM) to interact with the database. PORM is a simple and lightweight ORM 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.

composer require pionia/porm

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:

[db]
database =
username =
type =
host =
password =
port =

See the medoo database configuration for all the available options.

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.

; other settings

[db]
database =
username =
type =
host =
password =
port =

[db2]
database =
username =
type =
host =
password =
port =

; other settings

You can then specify the database connection to use when querying the database.

use Porm\Porm;

Porm::from('posts')->using('db2'); // will connect to the db2 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.


use Porm\Porm;

$instance = Porm::from('posts');
//other queries here

$database = $instance->database; // returns the Medoo instance

// this is also similar to
$database = $instance->getDatabase(); // returns the Medoo instance

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.


use Porm\Porm;

$instance = Porm::from('posts')
  ->save([
    'title' => 'My Post',
    'content' => 'This is my post content'
  ]);
// other queries here
$lastId = $instance->lastId();

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.

use Porm\Porm;

Porm::from('posts');

You can also alias the table name by passing the alias as the second argument.

use Porm\Porm;

Porm::from('posts', 'p');

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.


use Porm\Porm;

Porm::table('posts', 'p');

You can also define the connection to use at this point.

use Porm\Porm;
# will connect to the db2 database
Porm::from('posts', 'p', 'db2');

# or

Porm::table('posts', 'p', 'db2');

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.

use Porm\Porm;

Porm::from('posts')->columns('id', 'title');

# or

Porm::table('posts')->columns(['id', 'title']);

This can used whether getting one or multiple records.