erwang/korm

This package is abandoned and no longer maintained. No replacement package was suggested.

ORM for PHP

v2.0.0 2017-07-28 09:42 UTC

README

ORM in PHP ⛔️ DEPRECATED

Setup

The Connection class setup must be call first.

$connection = \KORM\Connection::setup('name','pdo_dsn', 'username', 'password');

A connection to a mysql database :

$connection = \KORM\Connection::setup('connectionName','mysql:host=localhost;dbname=database', 'username', 'password');

with options :

$connection = \KORM\Connection::setup('connectionName','mysql:host=localhost;dbname=database', 'username', 'password', array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));

Create a class

Each table in database requires a class with the same name :

class Table extends \KORM\Object{
}

name class is converted to lower case For example, to store books :

class Book extends \KORM\Object{
}

The Book objects will be store in book table

Define connection

Book::setConnection($connection);

Get a row from id

$book = new Book($id);

will load in $book all the data in table book with id=1

Create a row

$book = new Book();

Store an object

$book = new Book($id);
$book->title='Les Misérables';
$book->store();

Delete an object

$book = new Book($id);
$book->delete();

Find objects

Find one Object

$book = Book::findOne(['title'=>'Les Misérables']);

This return one Book (the first found)

Find multiple Objects

$authors = Author::find(['nationality'=>'French']);

This will return an array with all french authors

If you need complex where clause, you can use where

$books = Book::where('pages>:nbpages',['nbpages'=>100]);

This will return an array with books with more than 100 pages

If you want more complex queries :

$books = Book::query('select book.* from book,author where author.id=:author_id and pages>:nbpages and author.id=book.author_id',['nbpages'=>100,'author_id'=>1]);

This will return an array with books with more than 100 pages from author with id 1

$books = Book::getAll();

This will return all books in table

Relations

One to many

//create a book
$lesMiserables = new Book();
$lesMiserables->title='Les Misérables';
$lesMiserables->store();

//create an author
$hugo=new Author();
$hugo->name='Victor Hugo';
$hugo->store();

//create a relation
$lesMiserables->author=$hugo;
$lesMiserables->store();

//get the book
$book = new Book($lesMiserables->id);
$author = $book->author; //return the object Author from table author

many to many

//create tags
$tag1=new Tag();
$tag1->text='french';
$tag1->store();

$tag2=new Tag();
$tag2->text='Roman';
$tag2->store();
$lesMiserables->tag=[$tag1,$tag2];
$lesMiserables->store();

//find book from many
$booksWithFrenchTag = $tag1->book;

Count

//get the number of books
Book::count();

//get the number of books from an author
Book::count(['author_id'=>$author->id]);

Populate an object

//get data from an array
$post=['firstname'=>'Marcel','lastname'=>'Proust'];
//create a new author
$author=new Author();
$author->populate($post);
$author->store();

Truncate table

//with foreign key check
Author::truncate();
//without foreign key check
Author::truncate(false);

Drop table

//with foreign key check
Author::drop();
//without foreign key check
Author::drop(false);