Verbindungsdetails aus nem PDO Objekt lesen

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Verbindungsdetails aus nem PDO Objekt lesen

    Hey Community!

    Gibt es einen Weg um die Verbindungseigenschaften (Benutzername, Passwort, Datenbank, etc.) aus einem existierenden PDO Objekt zu lesen?

    PHP-Code:
    $pdo = new PDO('mysql:dbname=testdb;host=localhost''root');
    $username $pdo->getAttribute(..); //root 
    Thx in advance!
    Mein PHP Blog

  • #2
    Ist mir kein Weg bekannt.

    Kommentar


    • #3
      Habe leider auch nichts weiter in Erfahrung gebracht. :/
      Mein PHP Blog

      Kommentar


      • #4
        Wozu denn das ganze?

        Kommentar


        • #5
          Hab nen Migrator geschrieben für unsere Software (ähnlich wie Doctrine_Migration). Hier mal ein Beispiel-Script.

          PHP-Code:
          <?php
          class Migration4393 extends Brain_Migration_Script_Abstract
          {
              
          /**
               * Database connection
               *
               * @var Brain_Migration_Resource_Db
               */
              
          protected $_db null;
              
              
              
          /**
               * Init
               */
              
          public function init()
              {
                  
          $this->_db $this->migrateDb(new PDO('mysql:dbname=testdb;host=localhost''root'));
              }
              
              
              
          /**
               * Upwards
               */
              
          public function up()
              {
                  
          //executing a php script
                  
          $shell $this->migratePhpScript();
                  
          $shell->execute('ideajury-migration.php');
                  
                  
          //alternative: executing a php script
                  
          $shell $this->migrateShell('php');
                  
          $shell->execute('ideajury-migration.php');
                  
                  
          //making a mysql dumo
                  
          $shell $this->migrateShell('C:/Programme/xampp/mysql/bin/mysqldump');
                  
          $dump $shell->execute(array(
                      
          'add-drop-table' => null,
                      
          'user'           => 'root',
                      
          'testdb'
                  
          )); //-> --add-trop-table true --user root testdb
                  
          file_put_contents(tempnam(sys_get_temp_dir(), 'mysql-dump-'), $dump);
                          
                  
          //getting a table / creating a table
                  
          $tableName 'test2';
                  if (
          $this->_db->hasTable($tableName)) {
                      
          $table $this->_db->getTable($tableName);
                  }
                  else {
                      
          $table $this->_db->createTable($tableName, array(
                          
          'id' => array(
                              
          'type'         => 'int',
                              
          'length'       => '3',
                              
          'isPrimaryKey' => true,
                              
          'isAutoIncremented' => true
                          
          ),
                          
          'cdate' => array(
                              
          'type' => 'datetime'
                          
          )
                      ));
                  }
                  
                  
          //dropping a column if it exists
                  
          if ($table->hasColumn('cdate')) {
                      
          $table->getColumn('cdate')->drop();
                  }
                  
                  
          //adding a new column
                  
          $table->addColumn('title', array(
                      
          'type'   => 'varchar',
                      
          'length' => 150
                  
          ));
                  
                  
          //renaming a column
                  
          $newName uniqid();
                  
          $changedColumn $table->getColumn('title')->change(array(
                      
          'name' => $newName
                  
          ));
                  
                  
          //or using the rename() shortcut
                  
          $anotherNewName $newName '-newer';
                  
          $changedColumn->rename($anotherNewName);
                  
                  
          //changing a column
                  
          $changedColumn->change(array(
                      
          'type'    => 'int',
                      
          'length'  => mt_rand(112),
                      
          'default' => 1
                  
          ));
                  
                  
          //dropping primary key if it exists
                  
          if ($table->hasPrimaryKey())
                  {
                      
          //remove autoincrementation first
                      
          $table->getColumn('id')->change(array(
                          
          'isAutoIncremented' => false
                      
          ));
                      
                      
          $table->dropPrimaryKey();
                  }
                  
                  
          //adding primary key with mutiple columns
                  
          $table->addPrimaryKey(array('id'$anotherNewName));
              }
              
              
              
          /**
               * Downwards
               */
              
          public function down()
              {
                  
          $this->_db->getTable('test2')->drop();
              }
              
              
          }
          Jetzt möchte ich aber auch sowas machen können:
          PHP-Code:
          $this->_db->dump('foo.dump'); 
          Dazu muss ich aber "mysqldump" aufrufen und die Authparamter mitgeben können.
          Mein PHP Blog

          Kommentar


          • #6
            Naja, die kennst du in init ja. Da musst du dir diese halt mitnehmen. Alternative ist, eine eigene Unterklasse von PDO zu erben und diese Daten zu merken.

            Kommentar


            • #7
              Normalerweise müsste ich eben die Db gar nicht initialiseren.

              PHP-Code:
              $db $this->migrateDb(); //erstellt ne Instanz von Brain_Migration_Resource_Db() und setzt die Connection 
              würde die Connection von Propel holen, was wir für unsere DB Abstraktion verwenden.

              Was ich einzig könnte wäre anstatt ein PDO Objekt einfach die Auth Daten an Brain_Migration_Script_Abstract::migrateDb(), bzw. Brain_Migration_Resource_Db::__construct() mitzugeben. Die Default-Einstellungen die für Propel verwendet werden, kann ich auch aus der Config lesen. Find's halt einfach nicht so elegant. :/
              Mein PHP Blog

              Kommentar

              Lädt...
              X