Skip to content

Instantly share code, notes, and snippets.

@alganet
Last active August 30, 2017 13:34
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alganet/4957800 to your computer and use it in GitHub Desktop.
Respect\Relational Filtered, Mixed and Typed Collections

Basics

<?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!

New Collections

Filtered

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

Mixed Entities

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();

Typed Entities

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)
// ...
@filhodanuvem
Copy link

This source is right ?

<?php
$mapper = new Mapper(new PDO(/** your PDO config **/));

$posts = new Collection('post');
$posts->setMapper($mapper); // without this line, a RuntimeException occurred
$posts->fetchAll();

@hussani
Copy link

hussani commented Feb 19, 2013

I have the same problem when persist an object.

"Call to a member function setMapper() on a non-object"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment