开发者

Model Helper Zend

开发者 https://www.devze.com 2023-01-18 16:36 出处:网络
from what i know there is only action helper & view helper available at zend framework. 开发者_C百科

from what i know there is only action helper & view helper available at zend framework. 开发者_C百科

is there any model helper?

or how we can implement the model helper?


There's nothing in ZF actually called a Model helper - but if your model is accessing a database table you might want to create it as a class which extends Zend_Db_Table_Abstract. See examples in the ZF manual: http://framework.zend.com/manual/en/zend.db.table.html


In Zend Framework there is nothing defined for the models helpers like there is for the views helpers, however you can work around it and still preserve the application design (avoid using the library folder).

The way I do it is by adding a Helper folder inside the models one. Then you have to name your class like this:

class Application_Model_Helper_DateHelper
{
...
}

Then the autoloader will take care of finding it and loading it.

Unfortunately this naming is a little different from how you do it in the views folder. In the views folder you can create a folder named helpers and use a naming convention like:

class Zend_View_Helper_DarkBlueMenu extends Zend_View_Helper_Abstract 
{
...
} 

However, if you name the folder inside models as helpers then the classes inside it have to be named like this:

class Application_Model_helpers_DateHelper
{
...
}


First I add this line in my configuration file ie. application.ini:

includePaths.library                    = APPLICATION_PATH "/../library"

Then I add a class

class App_Model_Helper {   
    public static function resultAggregation($results) {}
    //.... all the helper you need
}

in a file placed in ..library\App\Model\Helper.php

This is the only way I found to factor the code I use in the model.

The helper method can then be called from the model:

App_Model_Helper::resultAggregation($results);

I am aware this breaks the OOD, so if any one has a better and cleaner solution I would greatly appreciate.

0

精彩评论

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