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:
php
$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:
php
$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 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 function testOpenClose() { $connection = $this->getConnection(false, false); $this->assertFalse($connection->isActive); $this->assertEquals(null, $connection->pdo); $connection->open(); $this->assertTrue($connection->isActive); $this->assertTrue($connection->pdo instanceof \PDO); $connection->close(); $this->assertFalse($connection->isActive); $this->assertEquals(null, $connection->pdo); $connection = new Connection(); $connection->dsn = 'unknown::memory:'; $this->setExpectedException('yii\\db\\Exception'); $connection->open(); }
/** * @param boolean $reset whether to clean up the test database * @param boolean $open whether to open test database * @return \yii\sphinx\Connection */ public function getConnection($reset = false, $open = true) { if (!$reset && $this->sphinx) { return $this->sphinx; } $db = new Connection(); $db->dsn = $this->sphinxConfig['dsn']; if (isset($this->sphinxConfig['username'])) { $db->username = $this->sphinxConfig['username']; $db->password = $this->sphinxConfig['password']; } if (isset($this->sphinxConfig['attributes'])) { $db->attributes = $this->sphinxConfig['attributes']; } if ($open) { $db->open(); } $this->sphinx = $db; return $db; }