37

ブートストラップ中にメインのデータベースアダプタへの参照をレジストリに登録して、サイトの他の場所で使用できるようにします(具体的には承認アクション)。

データベーステーブルオブジェクトを作成し、そのオブジェクトでgetAdapter()メソッドを呼び出してパススルーするという、醜い修正を実装しました。ただし、これは悪い方法であり、レジストリを介して利用できるようにしたいと思います。

誰かがこれを行う方法を知っていますか?正しい方向へのヘルプやポインタは大歓迎です!

乾杯スチュアート

追伸 ZendFramework1.8を使用しています。

4

9 に答える 9

71

Zend Framework 1.8+ を使用していて、コマンド ライン ツールを使用してプロジェクトを作成した場合は、構成application.iniファイルにデータベース設定を登録するだけです。

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "your.database.host"
resources.db.params.dbname = "database_name"
resources.db.params.username = "username"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true

データベース設定の前に が付けられているresources.db場合は、ファイルが自動的に実行されるため、ファイルで何もする必要さえありませBootstrap.phpん。また、設定isDefaultTableAdapterを に設定するとtrue、アプリケーション内の任意の場所でデータベース アダプタのインスタンスを取得できます。

$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
于 2009-11-24T00:09:56.500 に答える
12

返信ありがとうございます。受け入れられた回答を変更し、最終的に使用したソリューションを投稿することにしました-これは最終的に非常に簡単です!!

これは基本的にDcauntのコメントに基づいています...

ブートストラップクラスで..

protected function _initDb()
{
    $resource = $bootstrap->getPluginResource('db');

    $db = $resource->getDbAdapter();

    Zend_Registry::set("db", $db);
}

次に、他の場所にアクセスします...

$dbAdapter = Zend_Registry::get("db");

助けてくれてありがとう、うまくいけば、これは他の誰かに役立ちます。

于 2009-05-29T13:05:17.817 に答える
7

Your missing the best thing :)

If you use the Zend_Db_Table models (you should be) etc then you can set up a default adaptor - this way when you instantiate a model the DB connection it taken care off - this way you dont really need to save it in the registry or bother about connection before running a query through the model.

I do save it in the registry for later use if needed though - but I may remove this

protected function _initDB()
{
    // Check that the config contains the correct database array.
    if ($this->_config->db) {

        // Instantiate the DB factory
        $dbAdapter = Zend_Db::factory($this->_config->db);

        // Set the DB Table default adaptor for auto connection in the models
        Zend_Db_Table::setDefaultAdapter($dbAdapter);

        // Add the DB Adaptor to the registry if we need to call it outside of the modules.
        Zend_Registry::set('dbAdapter', $dbAdapter);
    }
}
于 2009-05-29T13:21:54.710 に答える
4

私の2セント...

デフォルトのDBアダプタを取得する方法:

ブートストラップから:

<?php    
$dbResource = $this->getPluginResource('db');
db = $dbResource->getDbAdapter();
var_dump($db);
?>

コントローラからは2つの方法があります:

<?php
// Method 1
$bootstrap = $this->getInvokeArg('bootstrap');
$dbResource = $bootstrap->getPluginResource('db');
$dbAdapter = $dbResource->getDbAdapter();
var_dump($dbAdapter);

// Method 2
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
var_dump($dbAdapter);
?>
于 2010-08-27T16:05:57.103 に答える
1

15.5.3.3でzend-documentationを確認してください。レジストリへのデータベースアダプタの保存

http://framework.zend.com/manual/en/zend.db.table.html

$db = Zend_Db::factory('PDO_MYSQL', $options);
Zend_Registry::set('my_db', $db);

// Later...

$table = new Bugs(array('db' => 'my_db'));

あなたが探しているようなもの?

編集:
iniファイルから構成をロードするには、次を使用します:
parse_ini_file($ inifile)

;configuration.ini
host = 127.0.0.1
user = username
password = blabla

;yourfile.php
$options = parse_ini_file('configuration.ini');

$db = Zend_Db::factory('PDO_MYSQL', $options);
于 2009-05-28T14:11:57.883 に答える
1

ブートストラップに、アダプタをレジストリに追加する方法があります。よりクリーンなソリューションを希望しますが、機能します:

protected function _initRegistry(){

    $this->bootstrap('db');
    $db = $this->getResource('db');

    $db->setFetchMode(Zend_Db::FETCH_OBJ);

    Zend_Registry::set('db', $db);
}
于 2009-05-28T15:48:13.603 に答える
1

Zend Framework 1.8 を使用している場合は、コントローラー/アクションで次のようにします。

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
    }
}

私の Defaul_Model_Users クラスは次のようになります。

<?php
/**
 * application/models/Users.php
 */
class Default_Model_Users extends Zend_Db_Table
{
    protected $_table;

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

    public function fetchAll()
    {
        $result = $this->getTable()->fetchAll();

        return $result;
    }
}

また、データベース テーブルと直接「対話」するモデルの部分は、DbTable ディレクトリ内にあり、次のようになります。

<?php
/**
 * application/models/DbTable/Users.php
 */
class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
    /** Table name */
    protected $_name = 'users';

    public function init()
    {
        $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
    }
}

次に、Zend Framework によって生成された同じ application.ini に、次の小さな追加を加えま​​す。

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"

それが、ブートストラップファイルを変更せずに行った方法です。

于 2009-06-09T06:44:40.080 に答える
1

これが私がすることです:

ブートストラップの内部:

define('CONFIG_FILE', '../config/general.ini');
define('APP_MODE', 'development');

イニシャライザの内部:

 /**
 * Initialize data bases
 * 
 * @return void
 */
public function initDb ()
{
    $options = Zend_Registry::get('conf');
    $db = Zend_Db::factory($options->database);
    $db->query(new Zend_Db_Expr('SET NAMES utf8'));
    Zend_Registry::set('db', $db);
}

public function initConfig ()
{
    if (file_exists(CONFIG_FILE) && is_readable(CONFIG_FILE)) {
        $conf = new Zend_Config_Ini(CONFIG_FILE, APP_MODE);
        Zend_Registry::set('conf', $conf);
    } else {
        throw new Zend_Config_Exception('Unable to load config file');
    }
}

最後に、私の設定ファイルは次のようになります。

[production]
database.adapter         = pdo_Mysql
database.params.host     = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname   = dbname

; Overloaded configuration from production

[development : production]
database.params.host     = localhost
database.params.username = root
database.params.password = 

を見てみましょう:

于 2009-05-28T15:59:04.300 に答える
1

アクセスできるはずのオブジェクトを格納するためにレジストリを使用したくなかったので、少し掘り下げました。Zend_Applicationのこのマニュアル ページで説明されているように、ブートストラップはフロント コントローラーのパラメーター "bootstrap" として登録されており、どのコントローラーからでもアクセスできます。

したがって、コントローラークラスでは、次のようにiniファイルで定義されたdbアダプターを取得できます。

$bootstrap = $this->getInvokeArg('bootstrap');
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
于 2009-09-24T17:54:26.093 に答える