开发者

No adapter found for App_Model_DbTable_User

开发者 https://www.devze.com 2023-03-19 19:52 出处:网络
I have very strange error I can\'t understand. I started a new project throw the zf.sh. And now when I try to ge the data from the database I get error for every table I have created when I\'m trying

I have very strange error I can't understand. I started a new project throw the zf.sh. And now when I try to ge the data from the database I get error for every table I have created when I'm trying to get data from it.

In this case I try to get a user that exist in the database.

Error:

Message: No adapter found for App_Model_DbTable_User
Stack trace:
#0 /Applications/MAMP/htdocs/offert/library/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract->_setupDatabaseAdapter()
#1 /Applications/MAMP/htdocs/offert/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract->_setup()
#2 /Applications/MAMP/htdocs/offert/application/models/UserMapper.php(9): Zend_Db_Table_Abstract->__construct()
#3 /Applications/MAMP/htdocs/offert/application/models/UserMapper.php(20): App_Model_UserMapper->setDbTable('App_Model_DbTab...')
#4 /Applications/MAMP/htdocs/offert/application/models/UserMapper.php(69): App_Model_UserMapper->getDbTable()
#5 /Applications/MAMP/htdocs/offert/application/controllers/AuthController.php(23): App_Model_UserMapper->find(3)
#6 /Applications/MAMP/htdocs/offert/library/Zend/Controller/Action.php(513): AuthController->loginAction()
#7 /Applications/MAMP/htdocs/offert/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('loginAction')
#8 /Applications/MAMP/htdocs/offert/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#9 /Applications/MAMP/htdocs/offert/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#10 /Applications/MAMP/htdocs/offert/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#11 /Applications/MAMP/htdocs/offert/public/index.php(26): Zend_Application->run()
#12 {main}  
Request Parameters:
array (
  'controller' => 'auth',
  'action' => 'login',
  'module' => 'default',
)  

Application.ini

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =

appnamespace = "App"

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

resources.db.adapter = "MYSQLI"
resources.db.params.dbname = "offert"
resources.db.params.host = "127.0.0.1"
resources.db.params.port = "8889"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.isDefaultTableAdapter = true

Bootstrap.php

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    // Init Autoload
    protected  function _initAutoload() {
        $autoloader = new Zend_Application_Module_Autoloader(array(
                'namespace' => 'App',
                'basePath' => dirname(__FILE__),
        ));

        $autoloader->addResourceType('acl', 'acl/', 'Acl');
        $autoloader->addResourceType('classes', 'classes', 'Class');

        return $autoloader;
    }

    // Plugin Controller
    protected function _initControllerPlugin() {
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new App_Plugin_Controller());
    }

    // Doctype
    protected function _initDoctype() {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    // Db
    protected function _initDb() {
        $resource = $this->getPluginResource('db');
        $db = $resource->getDbAdapter();
        $db->query('SET CHARACTER SET \'UTF8\'');
    }

}

User.php in开发者_JS百科 models/DbTable

<?php

class App_Model_DbTable_User extends Zend_Db_Table_Abstract {
    protected $_name = "user";
    protected $_primary = 'userID';
}

UserMapper.php

<?php

class App_Model_UserMapper {

    protected $dbTable;

    public function setDbTable($dbTable) {
        if(is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if(!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if(null == $this->dbTable) {
            $this->setDbTable('App_Model_DbTable_User');
        }
        return $this->dbTable;
    }

    /**
     * Find specified user
     * @param $id
     * @return App_Model_User
     */
    public function find($id) {

        $result = $this->getDbTable()->find($id);

        if(0 == count($result)) return;

        $row = $result->current();

        $user = new App_Model_User();
        $user->setUserID($row->userID)
            ->setFirstname($row->firstname)
            ->setLastname($row->lastname)
            ->setEmail($row->email)
            ->setPassword($row->password)
            ->setTelephone($row->telephone)
            ->setRole($row->role)
            ->setActive($row->active)
            ->setCreated($row->created)
            ->setLastLogin($row->lastLogin)
            ->setCompanyID($row->companyID);
        return $user;
    }

Someone who can help me out and see what's wrong with the code?

Thx!


The problem is your not bootstrapping db resource. By creating a method in your Bootstrap called _initDb your telling the Bootstrap that your method will handle the db resource bootstrap. So, the Bootstrap never calls, $this->bootstrap('db').

If you try to add, $this->bootstrap('db') inside your method with the name _initDb your going to get an exception Circular resource dependency detected.

So, the solution should be:

protected function _initMyDb() {
    $this->bootstrap('db');
    $resource = $this->getPluginResource('db');
    $db = $resource->getDbAdapter();
    $db->query('SET CHARACTER SET \'UTF8\'');
}

After bootstrap, you can start using Zend_Db_Table::getDefaultAdapter()


I fix this problem in my project put the code block dbAdpter of application.ini into tag production.

[production]

resources.db.adapter = "mysqli"
resources.db.params.host = "localhost"
resources.db.params.username = "db_user"
resources.db.params.password = "db_pass"
resources.db.params.dbname = "zf_cms"
resources.db.isDefaultTableAdapter = true

[staging : production]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号