0 follower

Final Class Yiisoft\Db\Mysql\DsnSocket

InheritanceYiisoft\Db\Mysql\DsnSocket
ImplementsStringable

Represents a Data Source Name (DSN) with unix socket for MySQL and MariaDB servers that's used to configure a Yiisoft\Db\Mysql\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 ''
$driver public property
public string $driver 'mysql'
$options public property
public array $options = []
$unixSocket public property
public string $unixSocket '/var/run/mysqld/mysqld.sock'

Method Details

Hide inherited methods

__construct() public method

public __construct( string $driver 'mysql', string $unixSocket '/var/run/mysqld/mysqld.sock', string $databaseName '', string[] $options = [] ): mixed
$driver string

The database driver to use.

$unixSocket string

The unix socket to connect to.

$databaseName string

The database name to connect to.

$options string[]

The options to use.

                public function __construct(
    public readonly string $driver = 'mysql',
    public readonly string $unixSocket = '/var/run/mysqld/mysqld.sock',
    public readonly string $databaseName = '',
    public readonly array $options = [],
) {}

            
__toString() public method

public __toString( ): string
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 DsnSocket('mysql', '/var/run/mysqld/mysqld.sock', 'yiitest', ['charset' => 'utf8mb4']);
$driver = new Driver($dsn, 'username', 'password');
$connection = new Connection($driver, $schemaCache);

Will result in the DSN string mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=yiitest;charset=utf8mb4.

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