I'm using Paris for my new project...
Let's say I have 3 tables: users, books and borrows:
users: id/name
books: id/title
borrows: users_id/books_id/borrow_date/return_date
In Books class:
function users()
{
return $this->has_many_through('Users', 'Borrows');
}
In Users class:
function books()
{
return $this->has_many_through('User', 'Borrows');
}
Everything is fine, I can access to borrowed books by each user and list of users who borrowed a single book before, but I'm wondering that how can I access to borrow_date
and return_date
column/property of Borrows table/class?
You can do it using Idiorm (the Paris brother).
User
model:
<?php
class User extends Model {
public static $_table = 'Users';
public static $_id_column = 'id';
function books()
{
return $this->has_many_through('User', 'Borrow');
}
public static function findBooksAndBorrows($id) {
return ORM::for_table('Users')
->join('Borrows', array('Users.id', '=', 'Borrows.users_id'))
->join('Books', array('Borrows.books_id', '=', 'Books.id'));
}
}
In your code:
$booksAndBorrows = User::findBooksAndBorrows(1)->find_array();
echo json_encode($booksAndBorrows);