Class yii\sphinx\Connection

Inheritanceyii\sphinx\Connection » yii\db\Connection
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-sphinx/blob/master/src/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

Property Type Description Defined By
$enableFloatConversion boolean Whether to enable conversion of the float query params into the direct literal SQL insertion. yii\sphinx\Connection
$lastInsertID string The row ID of the last row inserted, or the last value retrieved from the sequence object. yii\sphinx\Connection
$schemaMap yii\sphinx\Connection

Public Methods

Hide inherited methods

Method Description Defined By
createCommand() Creates a command for execution. yii\sphinx\Connection
escapeMatchValue() Escapes all special characters from 'MATCH' statement argument. yii\sphinx\Connection
getIndexSchema() Obtains the schema information for the named index. yii\sphinx\Connection
getLastInsertID() This method is not supported by Sphinx. yii\sphinx\Connection
getQueryBuilder() the query builder for this Sphinx connection yii\sphinx\Connection
getSchema() The schema information for this Sphinx connection yii\sphinx\Connection
quoteIndexName() Quotes a index name for use in a query. yii\sphinx\Connection
quoteTableName() Alias of quoteIndexName(). yii\sphinx\Connection

Property Details

Hide inherited properties

$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 property

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

public string $lastInsertID null
$schemaMap public property
public $schemaMap = [
    
'mysqli' => 'yii\sphinx\Schema',
    
'mysql' => 'yii\sphinx\Schema',
]

Method Details

Hide inherited methods

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

                public function createCommand($sql = null, $params = [])
{
    $command = new Command([
        'db' => $this,
        'sql' => $sql,
    ]);
    return $command->bindValues($params);
}

            
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.

                public function escapeMatchValue($str)
{
    return str_replace(
        ['\\', '/', '"', '(', ')', '|', '-', '!', '@', '~', '&', '^', '$', '=', '>', '<', "\x00", "\n", "\r", "\x1a"],
        ['\\\\', '\\/', '\\"', '\\(', '\\)', '\\|', '\\-', '\\!', '\\@', '\\~', '\\&', '\\^', '\\$', '\\=', '\\>', '\\<',  "\\x00", "\\n", "\\r", "\\x1a"],
        $str
    );
}

            
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.

                public function getIndexSchema($name, $refresh = false)
{
    return $this->getSchema()->getIndexSchema($name, $refresh);
}

            
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.

                public function getLastInsertID($sequenceName = '')
{
    throw new NotSupportedException('"' . __METHOD__ . '" is not supported.');
}

            
getQueryBuilder() public method

the query builder for this Sphinx connection

public yii\sphinx\QueryBuilder getQueryBuilder ( )
return yii\sphinx\QueryBuilder
getSchema() public method

The schema information for this Sphinx connection

public yii\sphinx\Schema getSchema ( )
return yii\sphinx\Schema
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

                public function quoteIndexName($name)
{
    return $this->getSchema()->quoteIndexName($name);
}

            
quoteTableName() public method

Alias of quoteIndexName().

public string quoteTableName ( $name )
$name string

Table name

return string

The properly quoted table name

                public function quoteTableName($name)
{
    return $this->quoteIndexName($name);
}