drix47193
drix47193
2015-10-22 18:51
浏览 45
已采纳

从Zend Framework 1中的应用程序对象获取数据库适配器

I am working on an application written in Zend Framework. I want to create a stand-alone API. I'm copying over from public/index.php, and here is the key code on that:

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$application->bootstrap()->run();

I have copied that over minus the run() directive, and now I'm trying to write db queries.. I've tried:

$application->_connection; //not declared, fails
$application->_db; //same deal
$application->select(); //same deal

I want to run things like:

$result = $application->_some_connection_object_but_where->query( .. );

Can you help me answer the "but where" part? Thanks

--EDITED INFO--

Also, to answer the great response I had on this, I do have a file called /application/Bootstrap.php with a class called:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

and this method for connection:

protected function _initDb()
{
    $appConfig = new Zend_Config_Ini('../application/configs/application.ini', APPLICATION_ENV);
    Zend_Registry::set('appConfig',$appConfig);

    $dbConfig = new Zend_Config_Ini('../application/configs/db.ini', APPLICATION_ENV);
    Zend_Registry::set('dbConfig',$dbConfig);
    $db = new Zend_Db_Adapter_Pdo_Mysql(array(
        'host' => $dbConfig->database->params->host,
        'username' => $dbConfig->database->params->username,
        'password' => $dbConfig->database->params->password,
        'dbname' => $dbConfig->database->params->dbname,
   ));
    $db->setFetchMode(Zend_Db::FETCH_ASSOC);
    $db->getConnection(); // force a connection... do not wait for 'lazy' connection binding later
    Zend_Registry::set('db',$db);

    Zend_Db_Table::setDefaultAdapter($db);

}

图片转代码服务由CSDN问答提供 功能建议

我正在研究用Zend Framework编写的应用程序。 我想创建一个独立的API。 我正在从 public / index.php 进行复制,以下是关键代码:

  / ** Zend_Application * / 
require_once  'Zend / Application.php'; 
 
 //创建应用程序,引导程序并运行
 $ application = new Zend_Application(APPLICATION_ENV,APPLICATION_PATH。'/ configs / application.ini'); 
nn.application->  bootstrap() - > run(); 
   
 
 

我已经通过减去 run()指令复制了它,现在我' 我试图编写数据库查询..我试过了:

  $ application-> _connection;  //未声明,失败
 $ application-> _db;  //同一笔交易
 $ application-> select();  //同一笔交易
   
 
 

我想运行以下内容:

  $ result = $ application->  _some_connection_object_but_where->查询(..); 
   
 
 

你能帮我回答“但在哪里”部分吗? 谢谢

- 编辑信息 -

另外,为了回答我对此的响应,我确实有一个名为的文件 /application/Bootstrap.php ,带有一个名为:

  class的类Bootstrap扩展Zend_Application_Bootstrap_Bootstrap 
   
 
 

和这个连接方法:

  protected function _initDb()
 {
 $ appConfig = new Zend_Config_Ini('../ application / configs / application.ini',APPLICATION_ENV  ); 
 Zend_Registry :: set('appConfig',$ appConfig); 
 
 $ dbConfig = new Zend_Config_Ini('../ application / configs / db.ini',APPLICATION_ENV); 
 Zend_Registry :: set(  'dbConfig',$ dbConfig); 
 $ db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host'=> $ dbConfig-> database-> params-> host,
'username'=> $  dbConfig-> database-> params-> username,
'password'=> $ dbConfig-> database-> params-> password,
'dbname'=> $ dbConfig->  database-> params-> dbname,
)); 
 $ db-> set  FetchMode(Zend_Db :: FETCH_ASSOC); 
 $ db-> getConnection();  //强制连接...不要等待'lazy'连接绑定以后
 Zend_Registry :: set('db',$ db); 
 
 Zend_Db_Table :: setDefaultAdapter($ db); 
 
  } 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulanyan6455
    doulanyan6455 2015-10-23 08:55
    已采纳

    If you have bootstrapped your resources in the "standard" way using references in your ./application/config/application.ini file of the form:

    resources.db.adapter = mysql
    resources.db.params.host = localhost
    // etc
    

    then you should be able to get the adapter object from the Zend_Application object using:

    $adapter = $application->getBootstrap()->getResource('db');
    

    Then you can write your db queries against that adapter.

    [Or - even better - feed that adapter into a model that encapsulates/hides the specific db-queries inside a well-defined interface whose implementations will be more testable.]

    Update

    Per request, here is an example of injecting a db-adapter into a model.

    class Application_Model_BaseModel
    {
        protected $db;
    
        public function __construct($db)
        {
            $this->db = $db;
        }
    
    }
    
    class Application_Model_Users extends Application_Model_BaseModel
    {
        public function getVerifiedUsers()
        {
            $select = $this->db->select()
                ->from('users')
                ->where(array(
                    'verified' => true,
                ));
            return $this->db->fetchAll($select);
        }    
    }
    

    Usage would then be:

    $model = new Application_Model_Users($db);
    $users = $model->getVerifiedUsers();
    

    This could be probably tightened further by using Zend_Db_Table_Abstract as the base model, but I intentionally provided a bare-bones example to show what I mean about injecting a db adapter into a model.

    点赞 评论

相关推荐