tags:

views:

58

answers:

2

I have a table of Hits, Articles and Categories
Now, a Hit belongs_to an Article/Category (depends on where it was done).
so I have a column on Hits table with the name 'parenttype'
That tells me 'Article' or 'Category'.
I wrote in the Hit model (extends ORM)

protected $_belongs_to= array(
    'page' => array('model'=> $this->parenttype)
    );

Now it complains about $this->parenttype not being expected?

A: 

How do you intend to access $this if the object is just about to be instantiated? ( even if you could, $this->parenttype definitely hasn't been loaded before relations were )

This means you need to define that relation some other way, a little bit later :) ( I still don't like the way you're doing it )

Kemo
+1  A: 

you should declare the variable protected $_belongs_to = NULL;

and on the constructor set it's value after calling the parent class constructor

public function __construct() {
    parent::__construct();
    $this->_belongs_to = array('page' => array('model' => $this->parenttype));
}
Marcel M.
worked like a charm, I think I understand it now.
Asaf