我有两张表,每张表都有Class Albums和Category,如下所示:
class Application_Model_DbTable_Albums extends Zend_Db_Table_Abstract
{
protected $_name = 'albums';
private $_custom_message = '';
protected $_referenceMap = array(
'CategoryTypes' => array(
'columns' => array('category_id'),
'refTableClass' => 'Category',
'refColumns' => array('id')
)
);
}
class Application_Model_DbTable_Category extends Zend_Db_Table_Abstract {
protected $_name = 'category';
protected $_dependentTables = array('Albums');
}
在我的IndexController编辑操作中,我有以下代码:
$category = new Application_Model_DbTable_Category();
$cat_id = $category->find(1);
$cat_3 = $cat_id->current();
$user_cat = $cat_3->findDependentRowset('Albums');
我收到此异常消息。注释掉$ user_cat将不会获得异常消息
Exception information:
Message: File "Albums.php" does not exist or class "Albums" was not found in the file
Stack trace:
#0 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\library\Zend\Db\Table\Row\Abstract.php(872): Zend_Db_Table_Row_Abstract->_getTableFromString('Albums')
#1 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\application\controllers\IndexController.php(187): Zend_Db_Table_Row_Abstract->findDependentRowset('Albums')
#2 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\library\Zend\Controller\Action.php(516): IndexController->editAction()
#3 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('editAction')
#4 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#5 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#6 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#7 C:\Program Files\Zend\Apache2\htdocs\zf-tutorial\public\index.php(49): Zend_Application->run()
#8 {main}
我的相册类在那里,但不知道为什么它说无法找到类或文件
答案 0 :(得分:2)
首先这是不正确的:
protected $_referenceMap = array(
'CategoryTypes' => array(
'columns' => array('category_id'),
'refTableClass' => 'Category',
'refColumns' => array('id')
)
);
refTableClass需要整个类名,而不是文件名:
protected $_referenceMap = array(
'CategoryTypes' => array(
'columns' => array('category_id'),
'refTableClass' => 'Application_Model_DbTable_Category',
'refColumns' => array('id')
)
);
依赖表格也是如此:
protected $_dependentTables = array('Application_Model_DbTable_Albums');
}
并确保$_name
属性的值始终与该类所代表的表的名称完全相同。
<强> [编辑] 强>
$category = new Application_Model_DbTable_Category();
$cat_id = $category->find(1);
$cat_3 = $cat_id->current();
//try
$user_cat = $cat_3->findDependentRowset();
//or
$user_cat = $cat_3->findDependentRowset('Application_Model_DbTable_Albums');
//or
$user_cat = $cat_3->findDependentRowset(new Application_Model_DbTable_Albums());