Getting around a “problem” with the naming conventions used by the Zend Framework autoloader

StackOverflow https://stackoverflow.com/questions/4463771

  •  11-10-2019
  •  | 
  •  

Question

Lets say that I have a table containing users. Each row in that table is thus a user. The important part here is the plural vs singular form.

Now, let's look at the we set up models for these in Zend Framework:

class Model_Users extends Zend_Db_Table_Abstract
{
  protected $_name      = 'users';
  protected $_primary   = 'user_id';
}

Now, let's further presume that we want another user object other than the standard row returned by the Zend_Db_Table, but we would still like to keep its functionality, just extend it. It would then make sense to implement and name a single user like:

class Model_User extends Zend_Db_Table_Row_Abstract
{
  ... Bunch of cool functions here :)
}

We then just add protected $_rowClass = 'Model_User'; to the Model_Users class and we're done... Normally that would be the case, but there seems to be a problem here in the way Zend Framework auto-loads the classes. Since we can have the folder structure /defaut/models/foo.php and name the class within that Model_Foo. The folder has a plural name but the class has a singular one. This seems to become a problem when I want to have the above stated structure. Since Zend Framework doesn't seem to be able to differentiate between the UserModel.php and UsersModel.php.

So the question to this long and somewhat poetic question is:

Is there a way to get around this WITHOUT starting to manually use includes?

Was it helpful?

Solution

Actually it is recommended to put your table classes in db folder within models, so it would be models/dbtable/users.php and class name Model_DbTable_Users for table, and models/user.php and Model_User for concrete user. But if you decide to skip on that I don't see the problem of having model/user.php with Model_User and models/users.php with Model_Users as table.

OTHER TIPS

Let me start off by saying I have never used the /model directory auto-loading directory structure. I personally prefer putting my models in a "library" that I create for that application. Here is an excerpt from my application.ini

includePaths.library = APPLICATION_PATH "/../library"    
autoloadernamespaces[] = "SNTrack_"

My user would be SNTrack_Model_User and located at /library/SNTrack/Model/User.php. I would call the table SNTrack_Model_UserTable.

Looks like a standard autoloader thing.

Make sure to initalize the autoloader, probably in Bootstrap.php, using something like:

protected function _initAutloader()
{
     $autoloader = new Zend_Application_Module_Autoloader(array(
         'basePath'    => APPLICATION_PATH,
          'namespace'  => '',
     ));
}

Then in application/models/User.php you define your class Model_User. And in application/models/Users.php you define your class Model_Users.

All should be fine. #FamousLastWords ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top