本文整理汇总了PHP中Doctrine\DBAL\DBALException::invalidPdoInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP DBALException::invalidPdoInstance方法的具体用法?PHP DBALException::invalidPdoInstance怎么用?PHP DBALException::invalidPdoInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\DBAL\DBALException
的用法示例。
在下文中一共展示了DBALException::invalidPdoInstance方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getConnection
/**
* Creates a connection object based on the specified parameters.
* This method returns a Doctrine\DBAL\Connection which wraps the underlying
* driver connection.
*
* $params must contain at least one of the following.
*
* Either 'driver' with one of the following values:
* pdo_mysql
* pdo_sqlite
* pdo_pgsql
* pdo_oracle
* pdo_sqlsrv
*
* OR 'driverClass' that contains the full class name (with namespace) of the
* driver class to instantiate.
*
* Other (optional) parameters:
*
* <b>user (string)</b>:
* The username to use when connecting.
*
* <b>password (string)</b>:
* The password to use when connecting.
*
* <b>driverOptions (array)</b>:
* Any additional driver-specific options for the driver. These are just passed
* through to the driver.
*
* <b>pdo</b>:
* You can pass an existing PDO instance through this parameter. The PDO
* instance will be wrapped in a Doctrine\DBAL\Connection.
*
* <b>wrapperClass</b>:
* You may specify a custom wrapper class through the 'wrapperClass'
* parameter but this class MUST inherit from Doctrine\DBAL\Connection.
*
* <b>driverClass</b>:
* The driver class to use.
*
* @param array $params The parameters.
* @param Doctrine\DBAL\Configuration The configuration to use.
* @param Doctrine\Common\EventManager The event manager to use.
* @return Doctrine\DBAL\Connection
*/
public static function getConnection(array $params, Configuration $config = null, EventManager $eventManager = null)
{
// create default config and event manager, if not set
if (!$config) {
$config = new Configuration();
}
if (!$eventManager) {
$eventManager = new EventManager();
}
// check for existing pdo object
if (isset($params['pdo']) && !$params['pdo'] instanceof \PDO) {
throw DBALException::invalidPdoInstance();
} else {
if (isset($params['pdo'])) {
$params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
} else {
self::_checkParams($params);
}
}
if (isset($params['driverClass'])) {
$className = $params['driverClass'];
} else {
$className = self::$_driverMap[$params['driver']];
}
$driver = new $className();
$wrapperClass = 'Doctrine\\DBAL\\Connection';
if (isset($params['wrapperClass'])) {
if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
$wrapperClass = $params['wrapperClass'];
} else {
throw DBALException::invalidWrapperClass($params['wrapperClass']);
}
}
return new $wrapperClass($params, $driver, $config, $eventManager);
}
示例2: getConnection
/**
* Creates a connection object based on the specified parameters.
* This method returns a Doctrine\DBAL\Connection which wraps the underlying
* driver connection.
*
* $params must contain at least one of the following.
*
* Either 'driver' with one of the following values:
*
* pdo_mysql
* pdo_sqlite
* pdo_pgsql
* pdo_oci (unstable)
* pdo_sqlsrv
* pdo_sqlsrv
* mysqli
* sqlanywhere
* sqlsrv
* ibm_db2 (unstable)
* drizzle_pdo_mysql
*
* OR 'driverClass' that contains the full class name (with namespace) of the
* driver class to instantiate.
*
* Other (optional) parameters:
*
* <b>user (string)</b>:
* The username to use when connecting.
*
* <b>password (string)</b>:
* The password to use when connecting.
*
* <b>driverOptions (array)</b>:
* Any additional driver-specific options for the driver. These are just passed
* through to the driver.
*
* <b>pdo</b>:
* You can pass an existing PDO instance through this parameter. The PDO
* instance will be wrapped in a Doctrine\DBAL\Connection.
*
* <b>wrapperClass</b>:
* You may specify a custom wrapper class through the 'wrapperClass'
* parameter but this class MUST inherit from Doctrine\DBAL\Connection.
*
* <b>driverClass</b>:
* The driver class to use.
*
* @param array $params The parameters.
* @param \Doctrine\DBAL\Configuration|null $config The configuration to use.
* @param \Doctrine\Common\EventManager|null $eventManager The event manager to use.
*
* @return \Doctrine\DBAL\Connection
*
* @throws \Doctrine\DBAL\DBALException
*/
public static function getConnection(array $params, Configuration $config = null, EventManager $eventManager = null)
{
// create default config and event manager, if not set
if (!$config) {
$config = new Configuration();
}
if (!$eventManager) {
$eventManager = new EventManager();
}
$params = self::parseDatabaseUrl($params);
// check for existing pdo object
if (isset($params['pdo']) && !$params['pdo'] instanceof \PDO) {
throw DBALException::invalidPdoInstance();
} else {
if (isset($params['pdo'])) {
$params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
} else {
self::_checkParams($params);
}
}
if (isset($params['driverClass'])) {
$className = $params['driverClass'];
} else {
$className = self::$_driverMap[$params['driver']];
}
$driver = new $className();
$wrapperClass = 'Doctrine\\DBAL\\Connection';
if (isset($params['wrapperClass'])) {
if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
$wrapperClass = $params['wrapperClass'];
} else {
throw DBALException::invalidWrapperClass($params['wrapperClass']);
}
}
if (self::$instance === null) {
self::$instance = 1;
//add new Cassandra types
Type::addType('cassandra_float', 'CassandraPDO4Doctrine\\Doctrine\\DBAL\\Types\\CassandraFloatType');
Type::addType('cassandra_datetime', 'CassandraPDO4Doctrine\\Doctrine\\DBAL\\Types\\CassandraDateTimeType');
}
return new $wrapperClass($params, $driver, $config, $eventManager);
}