Model Loading helper

14 messages Options
Embed this post
Permalink
PotatoBob

Model Loading helper

Reply Threaded More More options
Print post
Permalink

Normally models are loaded through require_once() or Zend_Loader::loadClass() with painfully long paths similar to "module/models/Model.php" or "module/models/controller/Model.php" and more likely "../models/controller/Model.php". After a few discussions on #zftalk with ralphschindler, SpotSec_Controller_Action_Helper_ModelLoader came to existence. ModelLoader does exactly what the name says, it makes loading models one step simpler by determining the path to the models folder so all you have to do is specify the model's class name.

Anyways I just wanted to see some feedback, as I never actually tested it and feel it is useless...

If you are using the conventional modular directory structure then ModelLoader is already setup and ready to go. If you are not then you can setup ModelLoader using __construct();


Usage:  $this->_helper->ModelLoader('UserModel');

link in proposal form: ModelLoading helper
--------
SpotSec
Bill Karwin from Zend

RE: Model Loading helper

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
Here's what I'm doing in the bootstrap (index.php) of a demo app I'm working on:
 
  require_once 'Zend/Loader.php';
  Zend_Loader::registerAutoload();
 
  $appDir = dirname(dirname(__FILE__)) . '/app';
  set_include_path($appDir . '/models' . PATH_SEPARATOR . get_include_path());
 
Following this, I have no problem loading models.  Isn't this simpler than using another helper class?  Am I missing something?
However, I realize that in more complex apps, it may not be quite this simple.
 
Regards,
Bill Karwin


From: PotatoBob [mailto:[hidden email]]
Sent: Monday, June 18, 2007 11:47 AM
To: [hidden email]
Subject: [fw-general] Model Loading helper

Normally models are loaded through require_once() or Zend_Loader::loadClass() with painfully long paths similar to "module/models/Model.php" or "module/models/controller/Model.php" and more likely "../models/controller/Model.php". After a few discussions on #zftalk with ralphschindler, SpotSec_Controller_Action_Helper_ModelLoader came to existence. ModelLoader does exactly what the name says, it makes loading models one step simpler by determining the path to the models folder so all you have to do is specify the model's class name.

Anyways I just wanted to see some feedback, as I never actually tested it and feel it is useless...

If you are using the conventional modular directory structure then ModelLoader is already setup and ready to go. If you are not then you can setup ModelLoader using __construct();


Usage:  $this->_helper->ModelLoader('UserModel');

link in proposal form: ModelLoading helper
--------
SpotSec

View this message in context: Model Loading helper
Sent from the Zend Framework mailing list archive at Nabble.com.
Ralph Schindler

Re: Model Loading helper

Reply Threaded More More options
Print post
Permalink
Bill Karwin wrote:

>  
> Following this, I have no problem loading models.  Isn't this simpler
> than using another helper class?  Am I missing something?
> However, I realize that in more complex apps, it may not be quite this
> simple.

Yep :)
If you are developing a modular and portable application, models will
exist in the module directory.  Like:
/application
   /user
     /modules
       /blog
         /controllers
         /models
         /views
       /default
         .... (so and so forth) ...
       /users
         .... (so and so forth) ...
       /forum
         .... (so and so forth) ...


So, given that senario, its hard to know at bootstrap time, all of the
different paths to all the different modules.  This helper allows you to
create more portable code.  Also, what if you don't want to use the
autoloader for purposes of APC or Bytecode?  This way, you are making
(effectively) the include_once call that comes along with loadClass.

This also allows you to jumpstart your applicaiton with pre-fabricated
functionality, and also allows you to "import" models from other modules
at development time.

-ralph
PotatoBob

RE: Model Loading helper

Reply Threaded More More options
Print post
Permalink
In reply to this post by Bill Karwin from Zend
Well, when you get into a modular directory structure, we don't have the simplicity of just setting the models folder for every model into the include_path. Then again we could do require_once('../models/MyModel.php');

Bill Karwin wrote:
Here's what I'm doing in the bootstrap (index.php) of a demo app I'm
working on:
 
  require_once 'Zend/Loader.php';
  Zend_Loader::registerAutoload();
 
  $appDir = dirname(dirname(__FILE__)) . '/app';
  set_include_path($appDir . '/models' . PATH_SEPARATOR .
get_include_path());
 
Following this, I have no problem loading models.  Isn't this simpler
than using another helper class?  Am I missing something?
However, I realize that in more complex apps, it may not be quite this
simple.
 
Regards,
Bill Karwin


________________________________

        From: PotatoBob [mailto:potatobob@gmail.com]
        Sent: Monday, June 18, 2007 11:47 AM
        To: fw-general@lists.zend.com
        Subject: [fw-general] Model Loading helper
       
       

        Normally models are loaded through require_once() or
Zend_Loader::loadClass() with painfully long paths similar to
"module/models/Model.php" or "module/models/controller/Model.php" and
more likely "../models/controller/Model.php". After a few discussions on
#zftalk with ralphschindler,
SpotSec_Controller_Action_Helper_ModelLoader came to existence.
ModelLoader does exactly what the name says, it makes loading models one
step simpler by determining the path to the models folder so all you
have to do is specify the model's class name.

        Anyways I just wanted to see some feedback, as I never actually
tested it and feel it is useless...

        If you are using the conventional modular directory structure
then ModelLoader is already setup and ready to go. If you are not then
you can setup ModelLoader using __construct();


        Usage:  $this->_helper->ModelLoader('UserModel');

        link in proposal form: ModelLoading helper
<http://svn.ralphschindler.com/repo/ZendFramework/Zend_Controller-ModelL
oader/library/Zend/Controller/Action/Helper/ModelLoader.php>  
        --------
        SpotSec <http://www.spotsec.com>  
       
________________________________

        View this message in context: Model Loading helper
<http://www.nabble.com/Model-Loading-helper-tf3942096s16154.html#a111815
98>
        Sent from the Zend Framework mailing list archive
<http://www.nabble.com/Zend-Framework-f15440.html>  at Nabble.com.
       
Ralph Schindler

Re: Model Loading helper (Everyone likes diagrams!)

Reply Threaded More More options
Print post
Permalink
In reply to this post by Bill Karwin from Zend
Thus fitting into this visual representation:

http://ralphschindler.com/tmp/ZF-MVC-Diagram.jpg


PS: we need more diagrams in ZF docs ;)
Maurice Fonk

Re: Model Loading helper

Reply Threaded More More options
Print post
Permalink
In reply to this post by Ralph Schindler
I too am all for keeping bootstraps small and uncluttered. We can all
benefit from code portability, and this small helper will just provide
that extra push. With the ever growing presence of conventions with
which we set up our applications, I believe that things like this
model-loader can be of use for us all.

While simply composing an app from pre-made modules may be something for
the future, it should be something we work towards,
MF

Ralph Schindler wrote:

> Bill Karwin wrote:
>
>>  
>> Following this, I have no problem loading models.  Isn't this simpler
>> than using another helper class?  Am I missing something?
>> However, I realize that in more complex apps, it may not be quite
>> this simple.
>
> Yep :)
> If you are developing a modular and portable application, models will
> exist in the module directory.  Like:
> /application
>   /user
>     /modules
>       /blog
>         /controllers
>         /models
>         /views
>       /default
>         .... (so and so forth) ...
>       /users
>         .... (so and so forth) ...
>       /forum
>         .... (so and so forth) ...
>
>
> So, given that senario, its hard to know at bootstrap time, all of the
> different paths to all the different modules.  This helper allows you
> to create more portable code.  Also, what if you don't want to use the
> autoloader for purposes of APC or Bytecode?  This way, you are making
> (effectively) the include_once call that comes along with loadClass.
>
> This also allows you to jumpstart your applicaiton with pre-fabricated
> functionality, and also allows you to "import" models from other
> modules at development time.
>
> -ralph
>
>
--

Maurice Fonk

[hidden email]
http://naneau.nl/

Scio me nihil scire

Shahar Evron

Re: Model Loading helper (Everyone likes diagrams!)

Reply Threaded More More options
Print post
Permalink
In reply to this post by Ralph Schindler
I generally like the idea as long as it is kept optional. I personally
like to load models by defining a model path (eg. APP_MODEL_DIR)
constant in my bootstrap file, and then call

Zend_Loader::loadClass('User', APP_MODEL_DIR);

whenever I need to. IMHO it's clean enough and super fast, and I don't
like the idea of loading an extra class and calling some methods just
for loading a model class.

Shahar.


On Mon, 2007-06-18 at 14:09 -0500, Ralph Schindler wrote:
> Thus fitting into this visual representation:
>
> http://ralphschindler.com/tmp/ZF-MVC-Diagram.jpg
>
>
> PS: we need more diagrams in ZF docs ;)
--
Shahar Evron [hidden email]
Technical Consultant
Zend Technologies

Mobile: +972 54 30 99 446
Office: +972  3 75 39 500 ext. 9546



signature.asc (196 bytes) Download Attachment
Ralph Schindler

Re: Model Loading helper (Everyone likes diagrams!)

Reply Threaded More More options
Print post
Permalink

Shahar Evron wrote:
> I generally like the idea as long as it is kept optional. I personally
> like to load models by defining a model path (eg. APP_MODEL_DIR)
> constant in my bootstrap file, and then call
>
> Zend_Loader::loadClass('User', APP_MODEL_DIR);

I completely agree with you there, and yes, it would be an optional
component of course, and what you describe works well in non-modular
environments.

> whenever I need to. IMHO it's clean enough and super fast, and I don't
> like the idea of loading an extra class and calling some methods just
> for loading a model class.

ModelLoader becomes important for fulfilling the role of finding the
path to models (usually based on some convention) that belong to an
in-use component.

The requirements this component fulfills is

a) ability to allow modules to take control of the loading of models and
model paths with the system

b) not require the bootstrap to be completely aware of all modules
conventions and paths to models

c) ability to register paths on on-demand and when modules are in-use

d) use Zend_Loader::loadClass() so that autoloading is not required,
thus allowing op-code caches better cacheability since LoadClass()
results in a require_once. (theoretically).


That being said, I still think there is a lot of value in such a component.

-ralph



Simon Mundy

Re: Model Loading helper (Everyone likes diagrams!)

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
Indeed Ralph

I've been envisioning a repository of 'modules' that developers could share with each other that could standalone. Like a forums module, a blog module, a generic login module, a feed generator module, a simple CMS table-editor module, etc...

So I download a module, untar it and drop it into my ZF site with a minimum of fuss. With components like these I can be reasonably sure that it is self-contained and references its own Models, Views, Helpers, etc without relying too much on what I've done with my bootstrap file.

May not happen soon but I do like the way ZF is shaping up to allow such a possibility.


Shahar Evron wrote:
I generally like the idea as long as it is kept optional. I personally
like to load models by defining a model path (eg. APP_MODEL_DIR)
constant in my bootstrap file, and then call
Zend_Loader::loadClass('User', APP_MODEL_DIR);

I completely agree with you there, and yes, it would be an optional component of course, and what you describe works well in non-modular environments.

whenever I need to. IMHO it's clean enough and super fast, and I don't
like the idea of loading an extra class and calling some methods just
for loading a model class.

ModelLoader becomes important for fulfilling the role of finding the path to models (usually based on some convention) that belong to an in-use component.

The requirements this component fulfills is

a) ability to allow modules to take control of the loading of models and model paths with the system

b) not require the bootstrap to be completely aware of all modules conventions and paths to models

c) ability to register paths on on-demand and when modules are in-use

d) use Zend_Loader::loadClass() so that autoloading is not required, thus allowing op-code caches better cacheability since LoadClass() results in a require_once. (theoretically).


That being said, I still think there is a lot of value in such a component.

-ralph

--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124


Axel Wüstemann

Re: Model Loading helper

Reply Threaded More More options
Print post
Permalink
In reply to this post by PotatoBob
Sorry bu how I get my model instance?

// $oKunde  is not an instance of KundeModel
// because ModelLoader::load returns end_Controller_Action_Helper_ModelLoader
$oKunde = $this->_helper->ModelLoader('KundeModel');

Thanks

Axel

ArsePit

RE: Model Loading helper

Reply Threaded More More options
Print post
Permalink
In reply to this post by PotatoBob
It sounds good to load modules as you need.
Right now for simplicity, I just created a section in config.ini for modules and then added some code to my bootstrap to take that information and automatically set the controllers and models for each module.

Currently, I do the following:
--- In config.ini ---
;;; modules ;;;
; paths are relative to the 'application' directory
modules.default.controllers.directory = "controllers"
modules.default.models.directory = "models"
modules.event.controllers.directory = "modules/event/controllers"
modules.event.models.directory = "modules/event/models"

--- In bootstrap ---
$router = $frontController->getRouter(); // returns a rewrite router by default
$aControllers = array();
// building associative array from config for module controller paths and adding models to the include path
foreach ($config->modules as $tmpName => $oConf) {
        $aControllers[$tmpName] = $app_path . $oConf->controllers->directory;
        set_include_path('.' . PATH_SEPARATOR . $app_path . $oConf->models->directory . PATH_SEPARATOR . get_include_path());
}
$frontController->setControllerDirectory($aControllers);
$registry->set('controllers', $aControllers); // add it to the registry
Kevin McArthur-2

Re: RE: Model Loading helper

Reply Threaded More More options
Print post
Permalink
Seems like a lot of configuration that should be implicit with the
common-modular-directory-structure?

----- Original Message -----
From: "ArsePit" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, June 27, 2007 9:46 AM
Subject: [fw-general] RE: Model Loading helper


>
> It sounds good to load modules as you need.
> Right now for simplicity, I just created a section in config.ini for
> modules
> and then added some code to my bootstrap to take that information and
> automatically set the controllers and models for each module.
>
> Currently, I do the following:
> --- In config.ini ---
> ;;; modules ;;;
> ; paths are relative to the 'application' directory
> modules.default.controllers.directory = "controllers"
> modules.default.models.directory = "models"
> modules.event.controllers.directory = "modules/event/controllers"
> modules.event.models.directory = "modules/event/models"
>
> --- In bootstrap ---
> $router = $frontController->getRouter(); // returns a rewrite router by
> default
> $aControllers = array();
> // building associative array from config for module controller paths and
> adding models to the include path
> foreach ($config->modules as $tmpName => $oConf) {
> $aControllers[$tmpName] = $app_path . $oConf->controllers->directory;
> set_include_path('.' . PATH_SEPARATOR . $app_path .
> $oConf->models->directory . PATH_SEPARATOR . get_include_path());
> }
> $frontController->setControllerDirectory($aControllers);
> $registry->set('controllers', $aControllers); // add it to the registry
>
> --
> View this message in context:
> http://www.nabble.com/Model-Loading-helper-tf3942096s16154.html#a11326850
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

Axel Wüstemann

Re: Model Loading helper

Reply Threaded More More options
Print post
Permalink
In reply to this post by Axel Wüstemann
Once again sorry for that question! The helper loads the model and I have to instatiate it... ;=)
Mark Maynereid

Re: Model Loading helper (Everyone likes diagrams!)

Reply Threaded More More options
Print post
Permalink
In reply to this post by Shahar Evron
How about this?
Zend_Loader::loadClass('Users', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'models');

I can almost hear the boos, but it works for a conventional modular layout.

Personally I'd like to see Zend_Controller_Action provide the info so I could do this:
Zend_Loader::loadClass('Users', $this->getModuleDirectory() . DIRECTORY_SEPARATOR . 'models');

No need for another helper class. No need to bloat your include_path or bloat your boostrap.

Correct me if I'm wrong but I think Padraic advocates read only model access from views so not sure that this would help in that, but I'm almost excited about the big throwdown at:
http://framework.zend.com/wiki/display/ZFDEV/Zend_View+Enhancement+VS.+Zend_Layout+Throwdown

I don't get out much :)

Regards,
Mark


Shahar Evorn wrote:
I generally like the idea as long as it is kept optional. I personally
like to load models by defining a model path (eg. APP_MODEL_DIR)
constant in my bootstrap file, and then call

Zend_Loader::loadClass('User', APP_MODEL_DIR);

whenever I need to. IMHO it's clean enough and super fast, and I don't
like the idea of loading an extra class and calling some methods just
for loading a model class.

Shahar.