David Yang Liu
David Yang Liu

Reputation: 1170

Zend No adapter found for Application_Model_DbTable_Users

I'm learning the Zend frame work, while going trough a tutorial i ran in to a No adapter found for Application_Model_DbTable_Users error. I can't seem to figure it out. here are my classes

class Application_Model_Register
 {
private $_dbTable;
private $db;

public function __construct()
{
    $this->_dbTable = new Application_Model_DbTable_Users();

        $parameters =array(
                        'host'     => '127.0.0.1',
                        'username' => 'root',
                        'password' => '******',
                        'dbname'   => 'tutblog',
                                                'adapter'  => 'Pdo_Mysql'
                   );
            try {
                $db = Zend_Db::factory('Pdo_Mysql', $parameters);
                $db->getConnection();
                } catch (Zend_Db_Adapter_Exception $e) {

                } catch (Zend_Exception $e) 
 }
 public function createUser($array)
{   var_dump($array);
    $_dbTable = new Application_Model_DbTable_Users();
    $this->_dbTable->insert($array);
    //insert('array') SQL commands-> form zen_db_table_abstract
}

DBtable Users's class

class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{

    protected $_name = 'users';
protected $_primary ='id';
protected $_password= 'password';
}

Other Users class is empty.

IndexController

public function indexAction()
{   
    $register =  new Application_Model_Register();
$register ->createUser(array( 'name' => 'becky',
                      'age'  => 25,
              'password' => 'somepw'
                 )2);*/
}

My ini file

[production]
phpSettings.display_startup_errors = 2
phpSettings.display_errors = 2
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

resources.db.params.adapter_ = "PDO_MYSQL"
resources.db.params.host ='localhost'
resources.db.params.dbname ='tutblog'
resources.db.params.username = "root"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true



[staging : production]


phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

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

resources.db.params.adapter_ = "PDO_MYSQL"
resources.db.params.host ='localhost'
resources.db.params.dbname ='tutblog'
resources.db.params.username = "root"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true

`Thank you guys in advance, i really don't know where to start new to the framework. I googled tried every thing, about to just restart the tut, but i feel like ill run in to this problem again.

thnx

Message: No adapter found for Application_Model_DbTable_Users

Stack trace:

ERROR CODE here

0 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract->_setupDatabaseAdapter()
#1 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract->_setup()
#2 /Applications/XAMPP/xamppfiles/htdocs/webs/zfNewTry/application/models/Register.php(10): Zend_Db_Table_Abstract->__construct()
#3 /Applications/XAMPP/xamppfiles/htdocs/webs/zfNewTry/application/controllers/IndexController.php(15): Application_Model_Register->__construct()
#4 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Controller/Action.php(513): IndexController->indexAction()
#5 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Controller/Dispatcher/Standard.php(289): Zend_Controller_Action->dispatch('indexAction')
#6 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#7 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#8 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#9 /Applications/XAMPP/xamppfiles/htdocs/webs/zfNewTry/public/index.php(26): Zend_Application->run()
#10 {main}  
Request Parameters:

array (
  'controller' => 'index',
  'action' => 'index',
  'module' => 'default',
)  
zf enable layout to get this template

Upvotes: 1

Views: 5536

Answers (3)

user3190469
user3190469

Reputation: 1

I am also facing the same problem when files moved to the development server though it is running on local server. Then i just followed the file naming convention.

  1. with all the following, look out for lower/upper case
  2. note that the models folder is plural whereas the class is Model singular
  3. make sure the class is named Application_Model_DbTable_Albums
  4. make sure the file is named Albums.php and in a folder named application/models/DbTable

you must go through it and check files are named properly (eg. Users.php for models).

It solves mine problem. Maybe yours.

Good Luck.

Upvotes: 0

RockyFord
RockyFord

Reputation: 8519

in your constructor you seem to be trying intialize a new db adapter. The DbTable model already use the adapter set in your application.ini so there is no need to setup a new adapter. Also I'm not sure it matters but the syntax for the adapter in the manual is *Pdo_Mysql*
If you need to use more then one adapter it would be more consistent to use the multiDb resource in your application.ini.

that all being said, a constructor for a model that uses the DbTable models would usually look something similar to:

  //This class is currently intended to be extended by concrete mappers
  protected $_tableGateway = NULL;

  protected $_identityMap = array();

 //this constructor typically provides the option to pass in a DbTable model
 // or a string value for the table name that would instantiate a Zend_Db_Table_Abstract
 public function __construct(Zend_Db_Table_Abstract $tableGateway = NULL) {

        if (is_null($tableGateway)) {
            //_tablename is the string value of the table, supplied by the concrete mapper
            $this->_tableGateway = new Zend_Db_Table($this->_tableName);
        } else {

            $this->_tableGateway = $tableGateway;
        }
    }

 protected function _getGateway() {

        return $this->_tableGateway;
    }

/**
     * findAll() is a proxy for the fetchAll() method and returns
     * an array of entity objects
     *
     * @return array returns an array of row objects
     */
    public function findAll() {

        $select = $this->_getGateway()->select();

        $rowset = $this->_getGateway()->fetchAll($select);
        $entities = array();
        foreach ($rowset as $row) {
            $entity = $this->_createEntity($row);
            $entities[] = $entity;
        }
        return $entities;
    }

    /**
     * Abstract method to be implemented by concrete mappers.
     */
    abstract protected function _createEntity($row);
}

I included a couple of method to help demonstrate how the Zend_Db_Table_Abstract interface can be used to write queries.

I found these links helpful while trying to figure this stuff out.

Domain Models
Integrating Data Mappers

And you can always find great stuff at Rob Allen's Akratbat.com

Good Luck.

Upvotes: 0

cypherabe
cypherabe

Reputation: 2603

if I remember correctly, you need to tell your application to always use the db connection as default connection for the Zend_Db_Table classes.

try putting this line after your db initialization: Zend_Db_Table::setDefaultAdapter($db)

I think you should do this before you instantiate the Zend_Db_Table class, so change your Application_Model_Register::__construct() to:

public function __construct()
{
    $parameters =array(
                    'host'     => '127.0.0.1',
                    'username' => 'root',
                    'password' => '******',
                    'dbname'   => 'tutblog',
                                            'adapter'  => 'Pdo_Mysql'
               );
     try {
         $db = Zend_Db::factory('Pdo_Mysql', $parameters);
         $db->getConnection();
     } catch (Zend_Db_Adapter_Exception $e) {

     } catch (Zend_Exception $e) { 
     }
     Zend_Db_Table::setDefaultAdapter($db);
     $this->_dbTable = new Application_Model_DbTable_Users();
}

Upvotes: 1

Related Questions