Introduction Last updated: 04/10/2020

Database tables are often related to one another. For example, a blog post may have many comments, or an order could be related to the user who placed it.
PHPtricks-ORM have a basic & simple relations system, makes managing and working with these relationships easy.

use PHPtricks\Orm\Model;
use MyProject\Namespace\Models\CommentModel;

class PostModel extends Model
{
    public function comments()
    {
        return $this->make($this)->relatedWith(CommentModel::class)
                                 ->join()
                                 ->on('post_id');
    }
}

$posts = new PostModel();

$post = $posts->find(1);

foreach( $post->comments() as $comment ) {
    echo $comment->body;
}



Definitions :

make({current model object})->relatedWith({model you wish to relate with current model})
                            ->join({join type [cross, left, right]})
                            ->on({foreign id on related table}, {id for current table});
INNER Join Not Supported Yet !



Cross Join

use PHPtricks\Orm\Model;
use MyProject\Namespace\Models\CommentModel;

class PostModel extends Model
{
    public function comments()
    {
        return $this->make($this)->relatedWith(CommentModel::class)
                                 ->join()
                                 ->on('post_id');
    }
}

$posts = new PostModel();

$post = $posts->find(1);

foreach( $post->comments() as $comment ) {
    echo $comment->body;
}



Left Join

use PHPtricks\Orm\Model;
use MyProject\Namespace\Models\CommentModel;

class PostModel extends Model
{
    public function comments()
    {
        return $this->make($this)->relatedWith(CommentModel::class)
                                 ->join('left')
                                 ->on('post_id');
    }
}

$posts = new PostModel();

$post = $posts->find(1);

foreach( $post->comments() as $comment ) {
    echo $comment->body;
}



Right Join

use PHPtricks\Orm\Model;
use MyProject\Namespace\Models\CommentModel;

class PostModel extends Model
{
    public function comments()
    {
        return $this->make($this)->relatedWith(CommentModel::class)
                                 ->join('right')
                                 ->on('post_id');
    }
}

$posts = new PostModel();

$post = $posts->find(1);

foreach( $post->comments() as $comment ) {
    echo $comment->body;
}



Left Outer Join

use PHPtricks\Orm\Model;
use MyProject\Namespace\Models\CommentModel;

class PostModel extends Model
{
    public function comments()
    {
        return $this->make($this)->relatedWith(CommentModel::class)
                                 ->join('left')
                                 ->outer()
                                 ->on('post_id');
    }
}

$posts = new PostModel();

$post = $posts->find(1);

foreach( $post->comments() as $comment ) {
    echo $comment->body;
}



Right Outer Join

use PHPtricks\Orm\Model;
use MyProject\Namespace\Models\CommentModel;

class PostModel extends Model
{
    public function comments()
    {
        return $this->make($this)->relatedWith(CommentModel::class)
                                 ->join('right')
                                 ->outer()
                                 ->on('post_id');
    }
}

$posts = new PostModel();

$post = $posts->find(1);

foreach( $post->comments() as $comment ) {
    echo $comment->body;
}



Full Outer Join

use PHPtricks\Orm\Model;
use MyProject\Namespace\Models\CommentModel;

class PostModel extends Model
{
    public function comments()
    {
        return $this->make($this)->relatedWith(CommentModel::class)
                                 ->fullOuterJoinOn('post_id');
    }
}

$posts = new PostModel();

$post = $posts->find(1);

foreach( $post->comments() as $comment ) {
    echo $comment->body;
}
please note : Relation class under BETA-version, too many methods coming soon ;).

don't forget , you can send me your suggestion and helps me out to develop something awesome


Do you have any suggestion ?

feel free to contact me on < m.anzawi2013@gmail.com >

Fork On Github

Fork PHPtricks-ORM Library on Github and help me to develop something awesome