时间:2021-07-01 10:21:17 帮助过:21人阅读
class BaseController extends Zend_Controller_Action{
public function init()
{
//初始化数据库适配器
$url = constant("APPLICATION_PATH").DIRECTORY_SEPARATOR.'configs'.DIRECTORY_SEPARATOR.'application.ini';
$dbconfig = new Zend_Config_Ini($url,'mysql');
$db = Zend_Db::factory($dbconfig->db);
$db->query('SET NAMES UTF8');
Zend_Db_Table::setDefaultAdapter($db);
//echo ""; 通过访问base控制器测试这里是有值的
//print_r($db);
//exit();
}
public function indexAction()
{
}
}
2 IndexController
require_once APPLICATION_PATH. '/models/tb_user.php';
require_once 'BaseController.php';
//有对数据库进行操作的控制器继承BaseController,没有对数据库进行操作的控制器继承Zend_Controller_Action
class IndexController extends BaseController
{
public function init()
{
//把下面的注释就没有问题,放开第一句的注释就会报错An error occurred Application error
$user = new tb_user();
$res = $user->fetchAll()->toArray(); //查询数据表tb_user中的数据
$this->view->res = $res; //将获得的数据分配给View
$this->render('index');
}
public function indexAction()
{
// action body
}
------解决方案--------------------
parent::init();//调用父类的构造函数试试
$user = new tb_user();