Object Relational Mapping

With object relational mapping, you don’t have to know structured query language (SQL) in order to write database queries. Liten and the ORM helps facilitate simple and rapid development and also allows both fluent sql queries as well as CRUD operations.

ORM

Instantiate

To create a new database connection, we need to instantiate the ORM class. Throughout the rest of the docs, we will use the variable $orm for the database connection.

Table

Connect to a database table by calling the table() method.

An alternative to above is calling the database table as a method.

Insert

When calling the insert(array $data) method, $data can be passed as one dimensional array to insert one new record or multiple arrays to insert multiple records.

Single entry:

Multiple entries:

Update

There are two ways to update a record, by using the active record pattern or by or by using the where clause.

Single entry:

The above can also be written as:

You can use the alternative save() instead of update().

Or you can use the set(array $data) or set($key, $value)

For multiple entries using set(array $data) and where($key, $value).

Save

Save() is a shortcut to insert() or update().

Insert:

Update:

Delete

Single entry:

Multiple entries:

Count

Count all the entries based on where() clause.

Use count for a specific column name.

Max

Max based on where() clause.

Min

Min based on where() clause.

Sum

Sum based on where() clause.

Avg

Average based on where clause.

Aggregate

Querying

The fluent query feature of the ORM allows you to write simple queries without having to write SQL.

FindOne

Returns a single record is found otherwise it will return false.

You can achieve the same above by using only the primary key and dropping the where clause.

Retrieving the entry

Find

Find returns and ArrayIterator of rows found, otherwise it will return false.

Find also accepts a closure ( find(Closure $callback) ) to perform data manipulation.

Fluent Query Builder

Select

Select all:

Select columns:

Where

Where can be used to setup the where clauses and they work with find(), findOne(), update(), and delete(). This is the same for the where aliases as well.Repetitive call to where and it’s aliases will append to each other using the AND operator. Use _or() to mimic the OR operator.

Examples:

There where aliases can help shorten the where examples above.

Primary key:

Not equal to:

Like:

Not like:

Greater than:

Greater than equal to:

Less than:

Less than equal to:

Where in:

Where not in:

Where null:

Where not null:

Where with OR and AND

Use _and_() / _or_() chained to any where clauses.

_and_():

_or_:

Order, Group, Limit, Offset

Joins

 

 

Last Modified: Apr 23, 2015 @ 11:33 PM

Liten Framework Forums Object Relational Mapping

  • You must be logged in to reply to this topic.