<?php

use Respect\Relational\Mapper;

$conn = new PDO('mysql:host=myhost;dbname=mydb', 'myuser', 'mypass');
$mapper = new Mapper($conn);

$postTwelve = $mapper->post[12]->fetch();
$commentsOnPostTwelve = $mapper->comment->post[12]->fetchAll();
$commentsOnPostsFromAuthorSeven = $mapper->comment->post->author[7]->fetchAll();

Zero config, no command-line generators, no entity classes needed. Just plug and rock on.

Respect\Relational uses, by default, simple and widespread naming conventions to infer your database schema. It doesn't query SHOW TABLES or anything like that to know how your schema works.

It also knows how to JOIN. The $commentsOnPostsFromAuthorSeven sample generates a single human readable query:

SELECT
    *
FROM 
    comment
INNER JOIN
    post
    ON comment.post_id = post.id
INNER JOIN
    author
    ON comment.author_id = author.id
WHERE
    author.id = 7;

All you need is a database that follow these three simple rules:

  • Every table must have a primary key named id
  • Every foreign key must have this format: table_id
  • Every N-to-N table must have this format: table_othertable

Of course this approach has limitations, but we're working on that. Different schema providers can be configured later to provide extra configuration, metadata information, caching and lazy loading (though most of these are not yet implemented).

The main goal is provide an ORM in which you can start simple and then gradually increase the complexity, only if needed.

See our README on GitHub to learn more!