ORM relationshpis
erralb opened this issue · 3 comments
Hi,
I am working with the Kohana ORM, and some of my models have belongs_to and has_many relationships.
When I build my rows, I can fetch the relationships and add them to the view, the problem is that it won't work with sorting / searching in server side processing, since these columns aren't actually columns of my ORM database table.
I was just wondering if you have an idea of how to address this problem...?
Hello @ierpe
Conceptually, keep in mind that this module is a wrapper for Paginate. Therefore, the question really revolves around whether or not we can manipulate the ORM object relationally within Paginate.
For reference, here is Paginate's documentation: http://dev.morgan.ly/kohana/v3.3/index.php/guide/paginate/basics
If you can select via the relationship, you should be able to sort or search it. This should work with the database builder using joins, but haven't tested with ORM. Again, the main thing here is being able to select. For example:
$account = ORM::factory('account');
$columns = array('account.id', 'account.title', 'account.created', 'user.name');
$paginate = Paginate::factory($account)
->columns($columns)
->search_columns($columns);
Give it a try and let me know how it goes.
Thank you,
Mícheál Morgan
Thanks for your answer and the clarification.
I got it working for my belongs_to relationships, here is an example ( the file model belongs_to a category through a column category_id )
$category_table = ORM::factory('Category')->table_name();
$files = ORM::factory('File')->join($category_table)->on('file.category_id', '=', $category_table.'.id');
$columns = array('file.id', 'file.name',$category_table.'.name');
$paginate = Paginate::factory($files)->columns($columns);
$datatables = DataTables::factory($paginate)->request($this->request)->execute();
I haven't figured yet a way for the has_many relationships, I'm running against the clock here so I guess I'll leave it as that for now and come back to it!
But I'm working on a "global" Datatables solution for the backend of a CMS module we're developing for Kohana, so I might modify your module at some stage to include relationships by default... :)
The key is being able to select individual columns. Glad it worked out. Let me know if you run into anything else. Thank you.