0

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?

gnarf
  • 105,192
  • 25
  • 127
  • 161
inquam
  • 12,664
  • 15
  • 61
  • 101
  • I'm not 100% sure what you are trying to get around... I'm going to try to answer by giving you an example of how I would setup that table and row class – gnarf Dec 16 '10 at 18:23

3 Answers3

1

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.

jamby77
  • 26
  • 1
  • The problem i that Zend Framework replies by saying:Fatal error: Class 'Model_Users' not found in... If I include both files manually everything works fine. And if I change the names of them so they don't 'collide' it also works fine. – inquam Dec 16 '10 at 18:50
0

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.

gnarf
  • 105,192
  • 25
  • 127
  • 161
  • I have this for core functionality in my application. But for module specific things the models will recide in the models folder since every module will be completly selfcontained except for the core functionality. – inquam Dec 16 '10 at 18:49
0

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 ;-)

David Weinraub
  • 14,144
  • 4
  • 42
  • 64