Class yii\sphinx\Connection

Inheritanceyii\sphinx\Connection » yii\db\Connection » yii\base\Component » yii\base\Object
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2-sphinx/blob/master/Connection.php

Connection represents the Sphinx connection via MySQL protocol.

This class uses PDO to maintain such connection. Note: although PDO supports numerous database drivers, this class supports only MySQL.

In order to setup Sphinx "searchd" to support MySQL protocol following configuration should be added:

searchd
{
    listen = localhost:9306:mysql41
    ...
}

The following example shows how to create a Connection instance and establish the Sphinx connection:

$connection = new \yii\db\Connection([
    'dsn' => 'mysql:host=127.0.0.1;port=9306;',
    'username' => $username,
    'password' => $password,
]);
$connection->open();

After the Sphinx connection is established, one can execute SQL statements like the following:

$command = $connection->createCommand("SELECT * FROM idx_article WHERE MATCH('programming')");
$articles = $command->queryAll();
$command = $connection->createCommand('UPDATE idx_article SET status=2 WHERE id=1');
$command->execute();

For more information about how to perform various DB queries, please refer to yii\sphinx\Command.

This class supports transactions exactly as "yii\db\Connection".

Note: while this class extends "yii\db\Connection" some of its methods are not supported.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$attributes array PDO attributes (name => value) that should be set when calling open() to establish a DB connection. yii\db\Connection
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$charset string The charset used for database connection. yii\db\Connection
$commandClass string The class used to create new database yii\db\Command objects. yii\db\Connection
$driverName string Name of the DB driver yii\db\Connection
$dsn string The Data Source Name, or DSN, contains the information required to connect to the database. yii\db\Connection
$emulatePrepare boolean Whether to turn on prepare emulation. yii\db\Connection
$enableFloatConversion boolean Whether to enable conversion of the float query params into the direct literal SQL insertion. yii\sphinx\Connection
$enableQueryCache boolean Whether to enable query caching. yii\db\Connection
$enableSavepoint boolean Whether to enable [savepoint](http://en. yii\db\Connection
$enableSchemaCache boolean Whether to enable schema caching. yii\db\Connection
$enableSlaves boolean Whether to enable read/write splitting by using $slaves to read data. yii\db\Connection
$isActive boolean Whether the DB connection is established yii\db\Connection
$lastInsertID string The row ID of the last row inserted, or the last value retrieved from the sequence object yii\sphinx\Connection
$master yii\db\Connection The currently active master connection. yii\db\Connection
$masterConfig array The configuration that should be merged with every master configuration listed in $masters. yii\db\Connection
$masterPdo PDO The PDO instance for the currently active master connection. yii\db\Connection
$masters array List of master connection configurations. yii\db\Connection
$password string The password for establishing DB connection. yii\db\Connection
$pdo PDO The PHP PDO instance associated with this DB connection. yii\db\Connection
$pdoClass string Custom PDO wrapper class. yii\db\Connection
$queryBuilder yii\db\QueryBuilder The query builder for the current DB connection. yii\db\Connection
$queryCache yii\caching\Cache|string The cache object or the ID of the cache application component that is used for query caching. yii\db\Connection
$queryCacheDuration integer The default number of seconds that query results can remain valid in cache. yii\db\Connection
$schema yii\db\Schema The schema information for the database opened by this connection. yii\db\Connection
$schemaCache yii\caching\Cache|string The cache object or the ID of the cache application component that is used to cache the table metadata. yii\db\Connection
$schemaCacheDuration integer Number of seconds that table metadata can remain valid in cache. yii\db\Connection
$schemaCacheExclude array List of tables whose metadata should NOT be cached. yii\db\Connection
$schemaMap array Mapping between PDO driver names and yii\sphinx\Schema classes. yii\sphinx\Connection
$serverRetryInterval integer The retry interval in seconds for dead servers listed in $masters and $slaves. yii\db\Connection
$serverStatusCache yii\caching\Cache|string The cache object or the ID of the cache application component that is used to store the health status of the DB servers specified in $masters and $slaves. yii\db\Connection
$shuffleMasters boolean Whether to shuffle $masters before getting one. yii\db\Connection
$slave yii\db\Connection The currently active slave connection. yii\db\Connection
$slaveConfig array The configuration that should be merged with every slave configuration listed in $slaves. yii\db\Connection
$slavePdo PDO The PDO instance for the currently active slave connection. yii\db\Connection
$slaves array List of slave connection configurations. yii\db\Connection
$tablePrefix string The common prefix or suffix for table names. yii\db\Connection
$transaction yii\db\Transaction The currently active transaction. yii\db\Connection
$username string The username for establishing DB connection. yii\db\Connection

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Object
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__sleep() Close the connection before serializing. yii\db\Connection
__unset() Sets a component property to be null. yii\base\Component
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
beginTransaction() Starts a transaction. yii\db\Connection
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
cache() Uses query cache for the queries performed with the callable. yii\db\Connection
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\Object
close() Closes the currently active DB connection. yii\db\Connection
createCommand() Creates a command for execution. yii\sphinx\Connection
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
escapeMatchValue() Escapes all special characters from 'MATCH' statement argument. yii\sphinx\Connection
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getDriverName() Returns the name of the DB driver. Based on the the current $dsn, in case it was not set explicitly by an end user. yii\db\Connection
getIndexSchema() Obtains the schema information for the named index. yii\sphinx\Connection
getIsActive() Returns a value indicating whether the DB connection is established. yii\db\Connection
getLastInsertID() This method is not supported by Sphinx. yii\sphinx\Connection
getMaster() Returns the currently active master connection. yii\db\Connection
getMasterPdo() Returns the PDO instance for the currently active master connection. yii\db\Connection
getQueryBuilder() Returns the query builder for the current DB connection. yii\db\Connection
getQueryCacheInfo() Returns the current query cache information. yii\db\Connection
getSchema() Returns the schema information for the database opened by this connection. yii\db\Connection
getSlave() Returns the currently active slave connection. yii\db\Connection
getSlavePdo() Returns the PDO instance for the currently active slave connection. yii\db\Connection
getTableSchema() Obtains the schema information for the named table. yii\db\Connection
getTransaction() Returns the currently active transaction. yii\db\Connection
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\base\Object
noCache() Disables query cache temporarily. yii\db\Connection
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
open() Establishes a DB connection. yii\db\Connection
quoteColumnName() Quotes a column name for use in a query. yii\db\Connection
quoteIndexName() Quotes a index name for use in a query. yii\sphinx\Connection
quoteSql() Processes a SQL statement by quoting table and column names that are enclosed within double brackets. yii\db\Connection
quoteTableName() Alias of quoteIndexName(). yii\sphinx\Connection
quoteValue() Quotes a string value for use in a query. yii\db\Connection
setDriverName() Changes the current driver name. yii\db\Connection
transaction() Executes callback provided in a transaction. yii\db\Connection
trigger() Triggers an event. yii\base\Component
useMaster() Executes the provided callback by using the master connection. yii\db\Connection

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
createPdoInstance() Creates the PDO instance. yii\db\Connection
initConnection() Initializes the DB connection. yii\db\Connection
openFromPool() Opens the connection to a server in the pool. yii\db\Connection
openFromPoolSequentially() Opens the connection to a server in the pool. yii\db\Connection

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_AFTER_OPEN \yii\db\Event An event that is triggered after a DB connection is established yii\db\Connection
EVENT_BEGIN_TRANSACTION \yii\db\Event An event that is triggered right before a top-level transaction is started yii\db\Connection
EVENT_COMMIT_TRANSACTION \yii\db\Event An event that is triggered right after a top-level transaction is committed yii\db\Connection
EVENT_ROLLBACK_TRANSACTION \yii\db\Event An event that is triggered right after a top-level transaction is rolled back yii\db\Connection

Property Details

$enableFloatConversion public property (available since version 2.0.6)

Whether to enable conversion of the float query params into the direct literal SQL insertion. This allows processing of the float values, since PDO does not provide specific param type for float binding, while Sphinx is unable to process float values passed as quoted strings.

$lastInsertID public read-only property

The row ID of the last row inserted, or the last value retrieved from the sequence object

public string getLastInsertID ( $sequenceName '' )
$schemaMap public property

Mapping between PDO driver names and yii\sphinx\Schema classes. The keys of the array are PDO driver names while the values the corresponding schema class name or configuration. Please refer to \yii\sphinx\Yii::createObject() for details on how to specify a configuration.

This property is mainly used by getSchema() when fetching the database schema information. You normally do not need to set this property unless you want to use your own yii\sphinx\Schema class to support DBMS that is not supported by Yii.

public array $schemaMap = ['mysqli' => 'yii\sphinx\Schema''mysql' => 'yii\sphinx\Schema']

Method Details

createCommand() public method

Creates a command for execution.

public yii\sphinx\Command createCommand ( $sql null, $params = [] )
$sql string

The SQL statement to be executed

$params array

The parameters to be bound to the SQL statement

return yii\sphinx\Command

The Sphinx command

escapeMatchValue() public method

Escapes all special characters from 'MATCH' statement argument.

Make sure you are using this method whenever composing 'MATCH' search statement. Note: this method does not perform quoting, you should place the result in the quotes an perform additional escaping for it manually, the best way to do it is using PDO parameter.

public string escapeMatchValue ( $str )
$str string

String to be escaped.

return string

The properly escaped string.

getIndexSchema() public method

Obtains the schema information for the named index.

public yii\sphinx\IndexSchema getIndexSchema ( $name, $refresh false )
$name string

Index name.

$refresh boolean

Whether to reload the table schema even if it is found in the cache.

return yii\sphinx\IndexSchema

Index schema information. Null if the named index does not exist.

getLastInsertID() public method

This method is not supported by Sphinx.

public string getLastInsertID ( $sequenceName '' )
$sequenceName string

Name of the sequence object

return string

The row ID of the last row inserted, or the last value retrieved from the sequence object

throws yii\base\NotSupportedException

always.

quoteIndexName() public method

Quotes a index name for use in a query.

If the index name contains schema prefix, the prefix will also be properly quoted. If the index name is already quoted or contains special characters including '(', '[[' and '{{', then this method will do nothing.

public string quoteIndexName ( $name )
$name string

Index name

return string

The properly quoted index name

quoteTableName() public method

Alias of quoteIndexName().

public string quoteTableName ( $name )
$name string

Table name

return string

The properly quoted table name