开发者

Kohana ORM, extending and parent methods

I'm just a newbe in Kohana ORM, so my question may be a bit silly for pro's, but.. :)

I have some ORM models, and all of them have a few the same methods like:

public function items_order_by_id($reverse = false)
{
   if($reverse) return $this-&开发者_JAVA技巧gt;order_by($this->_primary_key, 'desc')->find_all();

   else return $this->order_by($this->_primary_key, 'asc')->find_all(); 
}

OR

public function get_form()
{
    $result = array();

    foreach($this->_table_columns as $key => $value)
    {
        if($value['form']) $result[$key] = $this->_prefix.'_'.$key;
    }

    return $result;
}

If I'm adding a new model I must copy all similar methods, and If I want to modify any method I must modify all files. I know object programming has inheritance, but when I'm trying to extend Kohana_ORM I get exception for example:

The comment_id property does not exist in the Model_Comment class

And all another properities also do not exist.

Is it possible, to have a parent model which contain these methods?


Yes it is possible. You just have to make sure you write your functions so that they can be used by all Other models that will be extending it.

Most likely you are setting variables that don't exist for the Model you are running. That is why it's throwing errors.

Also this is a bit offtopic but you should take a look at AutoModeler. If you want a flexible Model system. https://github.com/zombor/Auto-Modeler

It has about the same functions as ORM but makes extending a lot easier.


Oh, It was definitely silly. I have just override _construct function and forgot parent::_construct().

So the problem is solved and now everything is ok.

Sorry for that :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜