Zend帮助中的多个表关系

时间:2010-05-02 14:36:37

标签: php zend-framework zend-db

我一直在做一些数据库映射来链接两个表无济于事。每次我运行代码时都会出现以下错误:

Message: File "Role.php" does not exist or class "Role" was not found in the file

Stack trace:

#0 C:\wamp\www\zend\library\Zend\Db\Table\Row\Abstract.php(867): Zend_Db_Table_Row_Abstract->_getTableFromString('Role')
#1 C:\wamp\www\uw\application\models\admin\User.php(56): Zend_Db_Table_Row_Abstract->findDependentRowset('Role')
#2 C:\wamp\www\uw\application\controllers\AdminController.php(110): Application_Model_Admin_User->getUsers()
#3 C:\wamp\www\zend\library\Zend\Controller\Action.php(513): AdminController->usersAction()
#4 C:\wamp\www\zend\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('usersAction')
#5 C:\wamp\www\zend\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#6 C:\wamp\www\zend\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#7 C:\wamp\www\zend\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#8 C:\wamp\www\uwi\public\index.php(26): Zend_Application->run()
#9 {main}

代码& DB下面:

应用/模型/管理/ user.php的     

class Application_Model_Admin_User extends Zend_Db_Table_Abstract
{
 protected $_name = 'user';
 protected $_dependentTables  = array('Role');

 public function getUsers()
 {  
  $rows = $this->fetchAll($this->select()->where('active = ?', 1));
  $rows1 = $rows->current();
  $rows2 = $rows1->findDependentRowset('Role');
  return $rows2;
 }
}

应用/模型/管理/ Role.php     

class Application_Model_Admin_Role extends Zend_Db_Table_Abstract
{
 protected $_name = 'role';
 protected $_referenceMap = array (
 'Role' => array(
  'columns'           => array('id'),
  'refTableClass'     => 'User',
  'refColumns'        => array('role_id')
 );

}

数据库表

CREATE TABLE role (
   id integer auto_increment NOT NULL,
   name varchar(120),
   PRIMARY KEY(id)
);

CREATE TABLE user (
   id integer auto_increment NOT NULL,
   username varchar(120),
   PRIMARY KEY(id),
   FOREIGN KEY(role_id) REFERENCES role(id)
);

1 个答案:

答案 0 :(得分:3)

错误消息为您提供了很好的提示!

您的应用程序中是否有名为“Role”的?或者它命名为Application_Model_Admin_Role?