<?php
use Respect\Relational\Mapper;
use Respect\Data\Collections\Collection;
//Configuring
$mapper = new Mapper(new PDO(/** your PDO config **/));
//SELECT post.* FROM post
$posts = $mapper->post->fetchAll();
//Behind the scenes
$posts = new Collection('post');
$posts->fetchAll();
//SELECT post.* FROM post WHERE post.id = 7
$posts = $mapper->post[7]->fetchAll();
//Behind the scenes
$posts = new Collection('post');
$posts->setCondition(7);
$posts->fetchAll();
//SELECT post.* FROM post WHERE post.title = 'Foo'
$posts = $mapper->post(array('title'=>'Foo'))->fetchAll();
//Behind the scenes
$posts = new Collection('post');
$posts->setCondition(array('title'=>'Foo'));
$posts->fetchAll();
//SELECT comment.*, post.* FROM comment INNER JOIN post ON comment.post_id = post.id
$commentsFromPost = $mapper->comment->post->fetchAll();
//Behind the scenes
$posts = new Collection('post');
$comments = new Collection('comment');
$comments->stack($posts);
$posts->fetchAll();
//CollectionShortcuts
$mapper->commentsFromPosts = $mapper->comment->post;
$mapper->commentsFromPosts[7]->fetchAll(); //Yay!
Limits which columns or entities will be selected.
Limiting columns. Works for fetching and persisting.
<?php
use Respect\Data\Collections\Filtered;
$mapper->postsTitles = Filtered::by('title')->post();
//SELECT comment.*, post.id, post.title FROM comment INNER JOIN post ON comment.post_id = post.id
$mapper->comment->postsTitles->fetchAll();
//INSERT INTO post (id, title) VALUES (NULL, 'New Post With Title Only')
$mapper->postsTitles->persist((object) array('id' => null, 'title' => 'New Post With Title Only'));
$mapper->postsTitles->flush();
Limiting entire entities (a way to use a join but don't bring it). Works for fetching and persisting.
<?php
use Respect\Data\Collections\Filtered;
$mapper->hiddenPost = Filtered::post();
//SELECT author.* FROM post INNER JOIN author ON post.author_id = author.id
$mapper->hiddenPost->author->fetchAll(); //brings only author
Mixes two or more tables into one entity:
<?php
use Respect\Data\Collections\Filtered;
$mapper->completeUser = Mixed::with('user_premium', 'user_blocked')->user;
//SELECT * FROM user INNER JOIN user_premium ON user.user_premium_id = user_premium.id
// INNER JOIN user_blocked ON user.user_blocked_id = user_blocked.id
$mapper->completeUser->fetchAll();
Applies single table inheritance. The type of this table will be determined by a discriminator column.
<?php
use Respect\Data\Collections\Filtered;
$mapper->entityNamespace = 'Genders\\';
$mapper->peopleStatsByGender = Typed::by('gender')->people;
//SELECT * FROM people
$mapper->peopleStatsByGender->fetchAll();
// Genders\\Male (id=1, gender=male)
// Genders\\Male (id=2, gender=male)
// Genders\\Female (id=3, gender=female)
// Genders\\Male (id=4, gender=male)
// Genders\\Female (id=3, gender=female)
// ...
This source is right ?