I've read about this issue before. I'm no ZF author, but my $.02 is,
even though this helps encapsulate your code into modules more
precisely, is that it's not good in practice. For one thing it creates
files with the same name that might be confusing. It also has multiple
versions of the same model, only one of which may be instantiated at a
time. My thought is if you run into this problem (as I just did on my
current project), then the application architecture requires a
different approach. On my current project I decided it made more sense
to move all my models out of "app/modules/module-name/models"
directories and into "app/models" than to strictly enforce module
encapsulation.
If however it is a must that you remain encapsulation and that these
modules are dropped into other apps and need to work this way, then I
would change the names of your models entirely. The Pages models could
then be named AdminPages and UserPages (avoiding the same model name).
This would imply the different model classes behave and act
differently (and may not require one another).
Good luck,
Eddie
---
Eddie Haber
Engineer + Architect
MetaFoundry, LLC
Email: [EMAIL PROTECTED]
Direct: (917) 865-0494
IM: eddiehaber
URL: http://www.metafoundry.com
---
On Sep 22, 2008, at 9:08 AM, chrisweb wrote:
Hello,
I have a noob questions for you ;-) ... i did not know if its better
to put
this question in the "core" or perhaps "zend_db" folder its why i
published
it here ... i also searched a lot in the mailinglist archive but did
not
find a similar question, sry if its an duplicate :blush:
my website structure is this one:
website
- application
-- default
--- controllers
---- IndexController.php
--- layout
--- models
---- Pages.php
--- views
---- scripts
----- index
------ index.phtml
-- admin
--- controllers
---- IndexController.php
--- layout
--- models
---- Apages.php
--- views
---- scripts
----- index
------ index.phtml
- library
- public
As you can see it, i have two similar models, the first model is
called
Pages.php, the second one has the same name Pages.php
In the beginning i had the following code in Default > controllers >
IndexController.php:
Zend_Loader::loadClass('Pages', '../application/default/models/');
... and the following code in Admin > controllers >
IndexController.php:
Zend_Loader::loadClass('Pages', '../application/admin/models/');
... everything worked fine, but then i wanted to try to use the
autoloader
for classes and files
its why i changed my bootstrap file, now its like this (with
autoloader):
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
// setup directories paths
set_include_path(
get_include_path() . PATH_SEPARATOR .
'../library' . PATH_SEPARATOR .
'../application/default/models' . PATH_SEPARATOR .
'../application/admin/models'
);
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
// Create registry object and setting it as the static instance in the
Zend_Registry class
$registry = new Zend_Registry();
Zend_Registry::setInstance($registry);
$dbconfig = new Zend_Config_Ini('../application/config.ini',
'database');
$registry->set('dbconfig', $dbconfig);
// setup database
$db = Zend_Db::factory($dbconfig->db);
Zend_Db_Table::setDefaultAdapter($db);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
$db->query('SET NAMES utf8');
Zend_Registry::set('db',$db);
// setup controller
$frontController = Zend_Controller_Front::getInstance();
$registry->set('front', $frontController);
$frontController->throwExceptions(false);
$frontController->setControllerDirectory(array(
'default' => '../application/default/controllers',
'admin' => '../application/admin/controllers'
));
$frontController->setParam('useDefaultControllerAlways', true);
$frontController->setRequest(new Zend_Controller_Request_Http());
// setup views
$view = new Zend_View();
Zend_Registry::set('View',$view);
// setup Zend Layout
Zend_Layout::startMvc();
// run!
$frontController->dispatch(); // dispatche!
In default > models > Pages.php i have the following code:
class Pages extends Zend_Db_Table {
protected $_name = 'pages'; // table name
protected $_primary = 'id'; // table primary key
protected $_sequence = true; // if auto increment is on
public function outputpageslist() {
$pagesTable = new Pages();
$query = "SELECT id, title FROM pages WHERE actif='1' AND
parent='0' ORDER
BY position";
$db = $pagesTable->getAdapter();
$result = $db->query($query);
return $result->fetchAll();
}
}
In admin > models > Pages.php i have the following code:
class Pages extends Zend_Db_Table {
protected $_name = 'pages'; // table name
protected $_primary = 'id'; // table primary key
protected $_sequence = true; // if auto increment is on
public function getpageslist() {
$pagesTable = new Pages();
$query = "SELECT id, title, date, actif, parent, position FROM
pages ORDER
BY position";
$db = $pagesTable->getAdapter();
$result = $db->query($query);
return $result->fetchAll();
}
}
The problem is that zend only seems to find the first one (default >
models
pages.php), zend does not find the second one (admin > models >
pages.php)
:confused:
I don't understand why, i guess its because zend first looks into
Default >
models and trys to find a file called Pages.php and then he goes to
Admin >
models ...
this is my Default > Controllers > IndexController.php code:
public function indexAction() {
$pagesTable = new Pages();
$outputpageslist = $pagesTable->outputpageslist();
$this->view->outputpageslist = $outputpageslist;
} // this works
this is my Admin > Controllers > IndexController.php code:
function indexAction() {
$pagesTable = new Pages();
$pageslist = $pagesTable->getpageslist();
$this->view->pageslist = $pageslist;
} this doesn't work: Error
The error message is this one:
Fatal error: Call to undefined method Pages::getpageslist() in
C:\xampp\htdocs\website\application\admin\controllers
\IndexController.php
As i said before i presume this error is normal, cause zend first
goes to
Default > modules and if he finds a file called Pages.php here, he
won't
open the file Admin > models > Pages.php, is this right?
If a change the name of one of the two models files, for example
Admin >
models > Pages.php to Admin > models > Adminpages.php it works
correctly, no
more error message
Another solution i found, would be to add this code to my bootstrap
file:
$module_in_use = $this->_request->getModuleName();
set_include_path(
get_include_path() . PATH_SEPARATOR .
'../library' . PATH_SEPARATOR .
'../application/'.$module_in_use.'/models'
);
(i like this solution because only the class i need is loaded and
not all
the classes ... but i dont know if it will always work, also with a
more
complex website, in every situation, for example if i would use action
stacks or stuff like that ?)
are those really solutions or is the error somewhere before this
point, is
there one solution which is better then the other? do you have any
tips to
avoid such problems in the future? %-|
Chris
--
View this message in context:
http://www.nabble.com/multiple-models-with-same-name-%28m1-models-pages.php-and-m2-models-pages.php%29-tp19607614p19607614.html
Sent from the Zend Framework mailing list archive at Nabble.com.