Bagaimana caranya memilih database yg ingin digunakan secara dinamik (seperti saat Kita login) ?

Untuk membuat form seperti diatas, buatlah module sfGuardAuth, sebagai contoh dapat melihat module sfGuardAuth di AlisJK.

Pada actions di sfGuatdAuth, perhatikan baris 30-43, berikut snippet-nya : 

$conn = dynamic_database::get_by_connection($request->getParameter("database")) ; $db_name = dynamic_database::get_dbname_from_dsn($conn["dsn"]) ; $this->getUser()->setAttribute('the_connection',$request->getParameter("database")); $this->getUser()->setAttribute('db_name',$db_name);

Nanti variabel session the_connection dan db_name akan digunakan pada filter. 

pada config/filters.yml tambahkan filter untuk fungsionalitas koneksi database dinamik ini :

# You can find more information about this file on the symfony website:
# http://www.symfony-project.org/reference/1_4/en/12-Filters

rendering: ~

remember_me:
  class: sfGuardRememberMeFilter
  
security:  ~

# insert your own filters here
dynamic_database:
  class: dynamicDatabaseFilter
cache:     ~
execution: ~

Buat kelas dynamicDatabaseFilter dan simpan di direktori lib/

<?php
class dynamicDatabaseFilter extends sfFilter
{
public function execute ($filterChain)
{
// Execute this filter only once
if ($this->isFirstCall())
{
// Code to execute before the action execution
$env = $this->getContext()->getConfiguration()->getEnvironment();
$configuration = $this->getContext()->getInstance()
->getConfiguration()
->getApplicationConfiguration(sfConfig::get('sf_app'),$env,0);

$myDatabaseManager = new myDatabaseManager($configuration);
$myDatabaseManager->initialize($configuration);
}

// Execute next filter in the chain
$filterChain->execute();
// Code to execute after the action execution, before the
//rendering

}

}

Pada snippet kode diatas ada kelas myDatabaseManager. berikut adalah snippet kode nya :

<?php

class myDatabaseManager extends sfDatabaseManager
{
        public function initialize(sfProjectConfiguration $configuration)
        {
                $the_connection     = sfContext::getInstance()->getUser()->getAttribute('the_connection') ;
        
                if(!isset($the_connection)) $the_connection = 'ppmb' ;
                //it's bad storing username, password in session, bisakan username password
                //dihapus setelah proses inisialisasi
                //$the_username = sfContext::getInstance()->getUser()->getAttribute('the_username') ;
                //$the_password = sfContext::getInstance()->getUser()->getAttribute('the_password') ;
        $conn          = dynamic_database::get_by_connection($the_connection) ;
        $the_username  = $conn["username"];
        $the_password  = $conn["password"]; 
        $db_name       = dynamic_database::get_dbname_from_dsn($conn["dsn"]);

                $parameters = array (
                                  "name"=>"doctrine",
                                  "dsn" => "mysql://{$the_username}:{$the_password}@localhost/{$db_name}") ;

                $database = new sfDoctrineDatabase($parameters);
                $database->initialize($parameters);
        }


Ada satu kelas yang penting disini, yaitu dynamic_database, kode kelas ini dapat dilihat di : 

Ok sudah, oh ya, jangan lupa, pada databases.yml jangan ada connection yang bernama doctrine, karena jika ada, maka kode diatas akan tidak berfungsi dan symfony tetap akan menggunakan connection doctrine yang tertulis di config/database.yml

Referensi 

  1. Best practice for multiple dynamic databases using symfony and Doctrine 7, https://groups.google.com/forum/#!topic/symfony-users/qfMlPlYtxT4
  2. Extra questions and solutions for sfGuardPlugin, http://www.symfonylab.com/extra-questions-and-solutions-for-sfguardplugin/
  3. Extending sfGuardPlugin (part 2), http://www.symfonylab.com/extending-sfguardplugin-part-2/
  4. Dynamically change database connection, http://markmail.org/message/eq22hjg3ufm3o4tr
  5. How to use multiple databases for different customers?, http://symfonyexperts.com/question/show/id/38
  6. How to drive multiple databases in one Diem project, http://community.diem-project.org/boards/8/topics/313

Comments