In Symfony 1.4 there were a lot of useful features that were documented and used by everyone back then. Just as many other frameworks of the time, there was a Connection string (data source name) where you needed to specify the database name, the address and the password, in the case of Symfony 1.4, this information was stored in the app/config/databases.yml
file under the param.dsn
key of the connection name:
# You can find more information about this file on the symfony website:
# http://www.symfony-project.org/reference/1_4/en/07-Databases
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: mysql:host=localhost;dbname=mydbname
username: root
password: 12345
This was of course one way to do it, using the mentioned DSN. However, there were as well configurations where you can easily find the connection parameters as simple properties in the YML file like this:
# You can find more information about this file on the symfony website:
# http://www.symfony-project.org/reference/1_4/en/07-Databases
all:
doctrine:
class: sfPropelDatabase
param:
# Database vendor
phptype: mysql
# Database host
hostspec: localhost
# Database name
database: mydbname
# Database username
username: root
# Database password
password: 12345
# Database port
port: 3306
# Database encoding
encoding: utf8
# Use persistent connections
persistent: true
No matter in which format is your connection string, in this article I will explain to you how to easily obtain the database name and other parameters of the connection in Symfony 1.4.
DSN Format
If your connection string is in the data source name format, you may obtain it by parsing this parameter with some code. In this case, we are going to obtain it from a simple controller. If you simply want to obtain the database name with a couple of lines of code, you may use the following code:
<?php
class exampleActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
// 1. Get current connection
$sfCurrentConnection = Doctrine_Manager::getInstance()->getCurrentConnection();
// Or alternatively, obtain a connection by its name
// $connectionName = "doctrine";
// $sfCurrentConnection = Doctrine_Manager::getInstance()->getConnection($connectionName);
// 2. Obtain connection parameters
$currentConnectionOptions = $sfCurrentConnection->getOptions();
// array(
// "dsn" => "mysql:host=localhost;dbname=mydbname",
// "username" => "myusername",
// "password" => "12345",
// "other" => array()
// )
var_dump($currentConnectionOptions);
// 3. Obtain the dsn parameter and parse it roughly with parse_ini_string
$sdsn = (string) $sfCurrentConnection->getOption('dsn');
$connectionData = parse_ini_string(str_replace(";", "\n", parse_url($sdsn)["path"]));
// $connectionData Data will contain the following parameters
//
// array(
// "host" => "localhost",
// "dbname" => "mydbname",
//)
var_dump($connectionData);
}
}
Parameters Format
Although this format isn't quite common, especially because you need to use Propel as ORM, you may obtain as well the parameters of the connection using the following code:
<?php
class exampleActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
// 1. Get current connection
$sfCurrentConnection = Doctrine_Manager::getInstance()->getCurrentConnection();
// Or alternatively, obtain a connection by its name
// $connectionName = "doctrine";
// $sfCurrentConnection = Doctrine_Manager::getInstance()->getConnection($connectionName);
// 2. Obtain connection parameters
$currentConnectionOptions = $sfCurrentConnection->getOptions();
// Obtain the data from the array or directly from the object
var_dump($currentConnectionOptions);
// $dbname = $sfCurrentConnection->getOption("database");
// array(
// "phptype" => "mysql",
// "hostspec" => "localhost",
// "database" => "mydbname",
// "username" => "login",
// "password" => "passwd",
// "port" => 80,
// "encoding" => "utf8",
// "persistent" => true
// )
}
}
Happy coding ❤️!