3.0 PHPixie ORM or a fresh perspective on ActiveRecord

image dolgoozernaya Already finished the third version of the PHPixie ORM component. He is now completely independent from the framework, and can safely be used by itself. In this regard, while we continue working on other components and written documentation, the developers made little tutorial in order to start working with ORM. Below I will provide its translation and add a few things myself, but first let's see how useful will delight us with this version:

the
    the
  • Most used ORM Model classes that are used to query and to represent the records in the database. For example do Kohana and Laravel. PHPixie shared model Repository, Entity and Query, each of which has strictly his destiny.
  • the
  • Except for SQL databases (SQLite, PostgreSQL, MySQL) is also fully supported by Mongo. You will be able to associate relationship model with SQL database models stored in Mongo collections.
  • the
  • to liaise between different bases ( such as between tables in different databases in MySQL ) many ORM to use separate queries instead of joins and subserosal. PHPixie uses subserosa always where possible.
  • the
  • Support built-in entities in Mongo
  • the
  • 97% covered by unit tests ( planned 100% by the end of this week), and 75% Funktsionalnyi tests.
  • the
  • Effective use of Query allows to reduce the number of queries to the database. For example you need to link all the topics of the author with some tags. In most ORM you will have to find topics, then tags and then associate them ( 3 of the DB query ). PHPixie allows you to do this in one query. By the way this is in the tutorial bottom


I hope I could be of interest to you, now look at the example links at the top translate:


the
<?php

require_once('vendor/autoload.php');

$config = new \PHPixie\Config();

//Initialize database component
$database = new \PHPixie\Database($config- > dataStorage(array(
'default' => array(
'driver' = > 'pdo',
'connection' = > 'sqlite::memory:'
)
)));

//Most ORM
$orm = new \PHPixie\ORM($database, $config- > dataStorage(array(
'relationships' => array(
array(
//Fairies can be many colors
'type' = > 'oneToMany',
'owner' => 'fairy',
'items' => 'flower'
)
)
)));


//Create labels
$connection = $database->get('default');

$connection->execute('
CREATE TABLE fairies (
id INTEGER PRIMARY KEY,
name VARCHAR(255)
)
');

$connection->execute('
CREATE TABLE flowers (
id INTEGER PRIMARY KEY,
name VARCHAR(255),
fairy_id INTEGER
)
');

//Models no longer exists
//Replace Repositories, Entities and Query

/*
We all hate when entities are also queries:
$fairy- > name = 'Trixie';
$fairy->save();
$fairy->where('name', 'Stella')->find();
*/

//Repository is created automatically for existing tablets
$fairyRepository = $orm- > get('fairy');
$flowerRepository = $orm- > get('flower');

//It was all initialization
//And now

//Create a few fairies

$trixie = $fairyRepository->create();
$a- > name = 'Trixie';
$a->save();

//Shorter version
$fairyRepository
->create(array('name' => 'Stella'))
->save();

//And we need flowers

foreach(array('Red', 'Yellow', 'Green', 'Purple') as $name) {
$flowerRepository
->create(array('name' => $name))
->save();
}


//Now let's  create  the Query
//analog WHERE `id` > 1 AND ( `name` = 'Green' OR `name` = 'Red')
$green = $flowerRepository->query()
->where('id', '>', 1)
->startAndWhereGroup()
- >where('name', 'Green')
- >or('name', 'Red')
- >endGroup()
->findOne();

//or more concise
$green = $flowerRepository->query()
->where('id', '>', 1)
- >and(function($q){
$q
- >where('name', 'Green')
- >or('name', 'Red')
})
->findOne();

//Communication
//Each link adds properties to Entities and Queries
//In our case the 'flowers' provides methods add(), remove() and removeAll()
//This is much easier than the methods addFlower(), removeFlower() and removeAllFlowers() on the object
$a->flowers- > add($green);

//When linking objects, the value is cached in both directions at once
//to avoid extra queries to the database
//In this case it seems to be so important as to achieve this effect

assert($green>fairy() == $a);

//Now let's try to tie all the colors except Green c fairy Stella
//And placed into a single request

//Define a query which would find Stella
$stellaQuery = $fairyRepository->query()
- >where('name', 'Stella');

//And the query which will find the flowers
$allExceptGreen = $flowerRepository->query()
- >where('name', 'Green');

//And now the magic
$stellaQuery->flowers- > add($allExceptGreen);


//Also Query allows to change entities without fetching
//For example, rename the flower Purple to Blue
$flowerRepository->query()
- >where('name', 'Purple')
->update(array(
'name' => 'Blue'
));

//You can find a fairy which has a specific flower:
$trixie = $fairyRepository->query()
- >relatedTo('flowers', $green)
->findOne();

//Now let's find all the fairies who have at least one flower
//Immediately load all of their flowers
$fairies = $fairyRepository->query()
- >relatedTo('flowers')
->find(array('flowers')); //this sets preloading relations

//And output them as simple objects
//Convenient for json_encode()
print_r($fairies- > asArray(true));


//More examples later =)


To try this example yourself simply:

the
git clone https://github.com/phpixie/orm
cd orm/examples

#if you do not have Composer
curl -sS https://getcomposer.org/installer | php

php composer.phar install
php quickstart.php


This ORM has been developed for more than a year and we are very interested in your questions and observations really looking forward to the review.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Fresh hay from the cow, or 3000 icons submitted!

Knowledge base. Part 2. Freebase: make requests to the Google Knowledge Graph

Group edit the resources (documents) using MIGXDB