0

Found a couple of links suggesting using 'ORM_Tree' to handle a self-referential table:

However, get an error from the controller saying 'Class ORM_Tree' not found. My model is as follows:

class Model_Article extends ORM_Tree {
    protected $children = "categories";
}

I am utilizing version 3.3.1 with the following in the controller:

class Controller_Category extends Controller{

    //View
    public function action_index(){
        $categories = ORM::factory('Category')->find_all();
        $view = new View('category/index');
        $view->set('category', $categories);
        $this->response->body($view);
    }
}

Was the class removed from version 3.3.1? If so, does anyone have suggestions for handling a self-referential table as shown below:

category_id - int(11) - No
category_name - varchar(45) - No
category_description - varchar(250) - Yes - NULL
__parent_id - int(11) - Yes - NULL

Hopefully, I got that table clear enough to understand. The __parent_id has a foreign-key relationship to the category_id. The __parent_id can be null (to allow for a root or even several top-level).

Any suggestions would be greatly appreciated as I had very high hopes to utilize the tree in a project. Please forgive if this has been answered elsewhere (please understand that I did not ask until I had 'googled' and even 'yahooed').

I am extremely new to Kahona as this framework seemed to be the best fit. I had examined somewhat Cakephp but did not care for their handling of AuthAcl - seemed a bit convoluted in that you had a 'tree' with a right and left node which I felt was unnecessarily complex. However, I have as yet not gotten to the AuthAcl of Kahona and may find that such an implementation is necessary.

kero
  • 10,647
  • 5
  • 41
  • 51

1 Answers1

0

There is no need for such a construct as you can easily design a self referencing ORM class. I'm not sure what you want, as the class is called Model_Article but the table has category_* columns.

So I will assume that you have a table categories with the given columns. Now a Model for this could look like this

class Model_Category extends ORM {
    protected $_table_name = "categories",
        $_has_many = array(
            'children' => array(
                'model' => 'Category',
                'far_key' => '__parent_id'
            )
        ),
        $_has_one = array(
            'parent' => array(
                'model' => 'Category',
                'foreign_key' => '__parent_id'
            )
        );
}

This is not tested but should at least give you the idea of the implementation. You can always read in the documentation or the API browser on how to use what. Also I would suggest playing around a bit with various settings if you feel unsatisfied.

If you wonder about whether to use far_key or foreign_key, this answer should help.

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51
  • This looks promising and I will most certainly give this a shot and get back to you. This is my first post on this site and hardly expected two replies within 20-min. – steven.henley Mar 05 '14 at 23:03
  • You're welcome! I currently don't have a running Kohana environment, so if it doesn't work on the first try, try it with changing `foreign_key`/`far_key` and `__parent_id`/`category_id` – kero Mar 05 '14 at 23:08