Connect Database with Zend_Db

For programming and general questions on Zend Framework

Connect Database with Zend_Db

Postby quan5292 on Fri Oct 25, 2013 9:34 am

Hi all,

I am beginning to learn ZF. I tried to work with the database by using Zend_Db (multi module) but it isn't work

application/configs/application.ini
Code: Select all
...
resources.frontController.moduleDirectory=APPLICATION_PATH "/modules"
resources.modules="" 
resources.db.adapter = "Pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "zend"

application/modules/admin/models/User.php
Code: Select all
<?php
class Admin_Model_User{
   protected $db;
   public function __contruct(){
      $this->db=Zend_Registry::get('db');
   }
   public function listall(){
      $sql=$this->db->query("select * from user order by id DESC");
      return $sql->fetchAll();
   }
}
?>

application/modules/admin/controllers/UserController.php
Code: Select all
class Admin_UserController extends Zend_Controller_Action{
   public function indexAction(){
      $muser=new Admin_Model_User;
      $data=$muser->listall();
      echo "<pre>";
        print_r($data);
        echo "</pre>";
   }
}

application/modules/admin/bootstrap.php
Code: Select all
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap{
   protected function _initAutoload(){
        $autoloader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => '',
                    'basePath' => dirname(__FILE__)
                ));
        return $autoloader;       
    }    
}

application/bootstrap.php
Code: Select all
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
   protected function _initDatabase(){
      $db=$this->getPluginResource('db')->getDbAdapter();
      Zend_Registry::set('db',$db);
   }
}


I get a error message "call to a member function query() on a non-object"

How must I config to fix this error?

Thanks!
quan5292
 
Posts: 2
Joined: Fri Oct 25, 2013 6:31 am

Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 3 guests