Registering Zend Database Adapter in Registry

后端 未结 9 2014
温柔的废话
温柔的废话 2020-12-23 00:07

I am looking to register a reference to the main Database Adapter in the Registry during Bootstrapping so it can be used elsewhere in my site (specifically the Authorisation

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 00:26

    If you are using Zend Framework 1.8 just do something like this in your controller/action:

    class CreateorderController extends Zend_Controller_Action
    {
        public function testAction()
        {
            //more code
            $users_obj = new Default_Model_Users(); //this would load the model using the Default namespace
            //more code
        }
    }
    

    My Defaul_Model_Users class would look something like this:

    _table) {
                $this->_table = new Default_Model_DbTable_Users();
            }
            return $this->_table;
        }
    
        public function fetchAll()
        {
            $result = $this->getTable()->fetchAll();
    
            return $result;
        }
    }
    

    And the part of the model which "interacts" directly with the database tables is found inside DbTable directory will look like this:

    _db->setFetchMode(Zend_Db::FETCH_OBJ);
        }
    }
    

    Then I would have the same application.ini generated by Zend Framework with this small addition:

    resources.db.adapter               = "PDO_MYSQL"
    resources.db.params.host           = "localhost"
    resources.db.params.dbname         = "mydb"
    resources.db.params.username       = "root"
    resources.db.params.password       = "password"
    

    That is how I did without without having to change the bootstrap files.

提交回复
热议问题