Relations - smeeckaert/Orm GitHub Wiki

Relations

Relations helps you build relations between models. At the time many/many relationships are not available

Configuration

Relations are set in the model.

use Orm\Model;

class User extends Model
{
    static protected $_prefix = 'user';
    protected static $_table = 'user';

    public $id;
    public $mail;
    public $pwd;
    public $login;

    protected static $_relations = array(
        'posts' => array(
            'from'       => 'id',  // user.user_id
            'to'         => 'user_id', // post.post_user_id
            'model'      => '\Api\Model\Post',
            'conditions' => array(
                'and_where' => array('published' => 1)
            )
        )
    );
}

Relations configuration is as below :

  • from : field from where the relation is made
  • to : field to where the relation is made
  • model: The linked model
  • conditions : anything you could pass to Model::find

Public methods

New public methods will be available once you have set relationships.

__get

Calling your relationship from the model will return you the matching elements.

var_dump($user->posts); // array (size=XXX) [0] => object(Api\Model\Post) ...

rel(name)

Create a new object from the relation, setting the foreign key for you.

$rel        = $user->rel('posts');
$rel->title = "Bar";
$rel->save(); // Will save the post for the user