Skip to content
This repository was archived by the owner on Oct 11, 2019. It is now read-only.

Getting Started

circlecreative edited this page Dec 16, 2014 · 1 revision

Basic Knowledge

If you have experience using RedBeanPHP or CodeIgniter Active Records it's won't be a big problems for understanding O2ORM. Basically O2ORM named the syntax almost same with them.

Main Concept

O2ORM is build for do more simply codes than others ORM or writing your own SQL codes from scratch. O2ORM is a good choice for building database tables with relationship or with hierarchical data.

Database Schema

O2ORM can help you create database schema on the fly.

$authors = O2ORM::create('authors');
$authors->set_structure = 'default';
$authors->name = ['Steeven Andrian','char','255','not null'];
$authors->website = 'http://www.steevenz.com';
$authors->biography = ['Steeven Andrian is one of legendary PHP Developer, he has made many Open Source PHP Frameworks','text',0,'null'];
O2ORM::store();

This will produced a SQL codes that running on the fly for create database table. Output:

CREATE TABLE `authors` (
  `id` int(11) NOT NULL,
  `created_date` datetime DEFAULT NULL,
  `created_user` int(11) DEFAULT NULL,
  `modified_date` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `modified_user` int(11) DEFAULT NULL,
  `checkin_date` datetime DEFAULT NULL,
  `checkin_user` int(11) DEFAULT NULL,
  `status` tinyint(1) NOT NULL,
  `ordering` int(11) DEFAULT NULL,
  `name` char(255) COLLATE utf8_unicode_ci NOT NULL,
  `website` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `biography` text COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

The table will automatically completed with additional fields, with default O2ORM table structures.

Working with relations

This code will show you as example for create table relations

$books = O2ORM::create('books');
$books->set_structure = 'default';
$books->title = ['Diving into O2ORM','varchar',255,'not null'];
$books->description = ['Complete O2ORM tutorial from basic to advanced','text',0,'null'];
$books->set_relations = ['authors:id:restrict'];
O2ORM::store();

Output:

CREATE TABLE `books` (
  `id` int(11) NOT NULL,
  `created_date` datetime DEFAULT NULL,
  `created_user` int(11) DEFAULT NULL,
  `modified_date` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `modified_user` int(11) DEFAULT NULL,
  `checkin_date` datetime DEFAULT NULL,
  `checkin_user` int(11) DEFAULT NULL,
  `status` tinyint(1) NOT NULL,
  `ordering` int(11) DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci,
  `authors_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_authors` (`authors_id`),
  CONSTRAINT `fk_authors` FOREIGN KEY (`authors_id`) REFERENCES `authors` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Clone this wiki locally