简体   繁体   English

Kohana ORM,扩展和父方法

[英]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.. :)我只是 Kohana ORM 的新手,所以我的问题对于专业人士来说可能有点傻,但是.. :)

I have some ORM models, and all of them have a few the same methods like:我有一些 ORM 型号,它们都有一些相同的方法,例如:

public function items_order_by_id($reverse = false)
{
   if($reverse) return $this->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.如果我要添加一个新的 model 我必须复制所有类似的方法,如果我想修改任何方法我必须修改所有文件。 I know object programming has inheritance, but when I'm trying to extend Kohana_ORM I get exception for example:我知道 object 编程有 inheritance,但是当我尝试扩展 Kohana_ORM 时,例如出现异常:

The comment_id property does not exist in the Model_Comment class Model_Comment class 中不存在comment_id属性

And all another properities also do not exist.并且所有其他属性也不存在。

Is it possible, to have a parent model which contain these methods?是否有可能拥有包含这些方法的父 model?

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.您很可能正在为正在运行的 Model 设置不存在的变量。 That is why it's throwing errors.这就是它抛出错误的原因。

Also this is a bit offtopic but you should take a look at AutoModeler.这也有点离题,但你应该看看 AutoModeler。 If you want a flexible Model system.如果您想要一个灵活的 Model 系统。 https://github.com/zombor/Auto-Modeler https://github.com/zombor/Auto-Modeler

It has about the same functions as ORM but makes extending a lot easier.它具有与 ORM 大致相同的功能,但使扩展更容易。

Oh, It was definitely silly.哦,这绝对是愚蠢的。 I have just override _ construct function and forgot parent:: _construct().我刚刚覆盖了 _构造 function 并忘记了 parent:: _construct()。

So the problem is solved and now everything is ok.所以问题解决了,现在一切正常。

Sorry for that:)对此感到抱歉:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM