views:

238

answers:

1

Hi all,

I just started with the version 3 of the Kohana Framework. I have worked a little with the $_has_many etc.

Now I have the table pages. The primary key is pageID. The table has a column called parentPageID. Now I want to make a ORM model who, when accesed like this $page->parent->find() returns the page identified by parentPageID.

I have the following already:

// Settings
protected $_table_name  = 'pages';
protected $_primary_key = 'pageID';
protected $_has_one = array(
    'parent' => array(
        'model'     => 'page',
        'foreign_key'   => 'parentPageID',
    ),
);

But that does not work, it simply returns the first page from the table. Last query says this:

SELECT `pages`.* FROM `pages` ORDER BY `pages`.`pageID` ASC LIMIT 1

Does somebody know how to solve this?

I know this can: $parent = $page->parent->find($page->parentPageID); but it must be and can be cleaner (I think).

Solved, see my answer below.

+1  A: 

I solved it my self. I needed to swap the things. I explain:

You can make a sentence like this: A page belongs to his parent page.

So, when I thought like that I know what i did wrong.

No I have this (what works perfectly):

protected $_belongs_to = array
(
    'parent' => array
    (
        'model' => 'page',
        'foreign_key' => 'parentPageID'
    )
);
protected $_has_many = array
(
    'childs' => array
    (
        'model' => 'page',
        'foreign_key' => 'parentPageID',
    )
);

I can use it like this:

$havesParent = $page->parent->loaded();
$childs = $page->childs->find_all()->as_array();
// ...

@Stackoverflow Sorry I filled your database with a question I answered my self.

VDVLeon