2010年11月22日星期一

Re: [fw-mvc] How to instantiate a model class to use its functions, standard solution?

Hi all,

Following the below, I managed to update the code successfully as follows.
However I do have questions below, would really appreciate a response:

---- bootstrap.php, updated as follows on the autoloader (see $resourceLoader
code below)
/* Set up autoload so we don't have to explicitely require each Zend Framework
class */

include_once('Zend/Loader/Autoloader.php');
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);

$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => '../application/',
'namespace' => 'My',
));
$resourceLoader->addResourceType('models', 'models/', 'Model');

---- new file: models/ProductTest.php -
class My_Model_ProductTest {
}

---- to call the above from the controller, I do this:
$model_productlist = new My_Model_ProductTest();

I'm relieved it is working, but I'd like to know:
a) If I want to store a few functions in a class, and eventually build up that
class as a Product List class (for the list page for product thumbnails), should
I start here, by creating a model? Or is there another recommendation to store
functions in a common place?
b) Following my mail below, should bootstrap.php be a class, or just have its
own inline code as in my script? The version of ZF I am using is 1.10.2.
c) Although documentation online explains that Autoloading is a good way to go,
I'd just like some opinions from those who have tested performance on this. Is
it 'better' to 'include_once' the class file you want to use, or use ZF's
autoloading for that class as explained above? Or does performance not matter,
and it is a preference of the developer?

d) In the above, I've used the namespace "My". How can I use ZF's default?
(since that would be more straightforward)? e.g. I tried
Application_Model_ProductTest as the class name but there was an error that ZF
could not load /Application/Model/ProductTest.php


e) Is there a way I can get and set controller variables from the controller
action/function that instantiated the class? For example can I set the variable
$this->controller->product_array = $myarray;
from within my function in the model?
I guess the recommended way is to return the variable from the function rather
than access any controller variables.

Many thanks,
Rishi

________________________________
From: Rishi Daryanani <rishijd@yahoo.com>
To: fw-mvc@lists.zend.com
Sent: Tue, November 23, 2010 10:04:41 AM
Subject: [fw-mvc] How to instantiate a model class to use its functions,
standard solution?

Hi,

I've been using Zend Framework in a somewhat 'partial' way between Zend's MVC
logic and our own coding structure (classes and shared functions). This has
been done using controllers and views, with the controller doing most of the
work (as opposed to the model). We have a new feature which required shared
functions again, and I'd like to put these in its own class which can be
instantiated when needed - ie. a model. (I'd like to do this to learn how to
code a model, for my own reference and better coding for the future).

The model needs to simply be a class with a few functions. It does not need to
extend Zend_Db or anything (as we are currently using our own functions for
database management) but it would be nice for it to set variables in
$this->controller.

My simple question - if I put the model in the 'application/models/' folder can

call it say 'ProductList.php' - how do I update bootstrap.php to set the
filepath to this model? Or do I need to include the file explicitly in the
controller? I can't seem to find a simple guide online for this.

FYI, my current bootstrap.php file contains the following, on Zend Framework
version 1.10.2 (recently upgraded from an older version):
/* Add the Zend Framework library to the include path so that we can access the
ZF classes */

set_include_path('../library' . PATH_SEPARATOR . get_include_path());

/* Set up autoload so we don't have to explicitely require each Zend Framework
class */

include_once('Zend/Loader/Autoloader.php');
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);

Online references show that the autoloader in bootstrap is more efficient than
doing an explicit include -
- I just want to know typically what/how you would name and instantiate a model?

Or do I need to go about this another way?
- I assume the solution to the above is very simple, so I'd just need to
know\understand the code to use to do this.
- Note that references like the below show bootstrap "classes", but our current
bootstrap.php file has code straightaway like the above, and not in a class.

References so far:
http://devzone.zend.com/article/4525-Developing-a-Comprehensive-Autoloader
http://framework.zend.com/manual/en/zend.loader.autoloader.html - good for
function references
http://www.youtube.com/watch?v=kuNNwpeNn4s
http://techblog.wimgodden.be/2008/12/21/autoloading-in-zend-framework-how-not-to-do-it/

Many thanks
Rishi

没有评论: