0 follower

Final Class Yiisoft\Db\Pgsql\Dsn

InheritanceYiisoft\Db\Pgsql\Dsn
ImplementsStringable

Represents a Data Source Name (DSN) for a PostgreSQL Server that's used to configure a {@see Driver} instance.

To get DSN in string format, use the (string) type casting operator.

Property Details

Hide inherited properties

$databaseName public property
public string $databaseName 'postgres'
$driver public property
public string $driver 'pgsql'
$host public property
public string $host '127.0.0.1'
$options public property
public array $options = []
$port public property
public string $port '5432'

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $driver 'pgsql', string $host '127.0.0.1', string $databaseName 'postgres', string $port '5432', string[] $options = [] )
$driver string

The database driver name.

$host string

The database host name or IP address.

$databaseName string

The database name to connect to.

$port string

The database port. Empty string if not set.

$options string[]

The database connection options. Default value to an empty array.

                public function __construct(
    public readonly string $driver = 'pgsql',
    public readonly string $host = '127.0.0.1',
    public readonly string $databaseName = 'postgres',
    public readonly string $port = '5432',
    public readonly array $options = [],
) {}

            
__toString() public method

public string __toString ( )
return string

The Data Source Name, or DSN, has the information required to connect to the database.

Please refer to the PHP manual on the format of the DSN string.

The driver property is used as the driver prefix of the DSN, all further property-value pairs or key-value pairs of options property are rendered as key=value and concatenated by ;. For example:

$dsn = new Dsn('pgsql', '127.0.0.1', 'postgres', '5432', ['sslmode' => 'disable']);
$driver = new Driver($dsn, 'username', 'password');
$connection = new Connection($driver, $schemaCache);

Will result in the DSN string pgsql:host=127.0.0.1;dbname=postgres;port=5432;sslmode=disable.

                public function __toString(): string
{
    $dsn = "$this->driver:host=$this->host";
    if ($this->databaseName !== '') {
        $dsn .= ";dbname=$this->databaseName";
    }
    if ($this->port !== '') {
        $dsn .= ";port=$this->port";
    }
    foreach ($this->options as $key => $value) {
        $dsn .= ";$key=$value";
    }
    return $dsn;
}