From 0839ceb598510dcc9506132b875cb95dff0abccc Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Wed, 13 Nov 2013 20:04:07 +0200 Subject: [PATCH] Parameter "options" added to "yii\sphinx\Command::update()" --- extensions/sphinx/Command.php | 5 +++-- extensions/sphinx/Connection.php | 2 +- extensions/sphinx/QueryBuilder.php | 12 +++++++++-- tests/unit/extensions/sphinx/CommandTest.php | 30 ++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/extensions/sphinx/Command.php b/extensions/sphinx/Command.php index c54861f..cab9487 100644 --- a/extensions/sphinx/Command.php +++ b/extensions/sphinx/Command.php @@ -453,11 +453,12 @@ class Command extends Component * @param string|array $condition the condition that will be put in the WHERE part. Please * refer to [[Query::where()]] on how to specify condition. * @param array $params the parameters to be bound to the command + * @param array $options list of options in format: optionName => optionValue * @return static the command object itself */ - public function update($index, $columns, $condition = '', $params = []) + public function update($index, $columns, $condition = '', $params = [], $options = []) { - $sql = $this->db->getQueryBuilder()->update($index, $columns, $condition, $params); + $sql = $this->db->getQueryBuilder()->update($index, $columns, $condition, $params, $options); return $this->setSql($sql)->bindValues($params); } diff --git a/extensions/sphinx/Connection.php b/extensions/sphinx/Connection.php index bb498d4..6009f1f 100644 --- a/extensions/sphinx/Connection.php +++ b/extensions/sphinx/Connection.php @@ -14,7 +14,7 @@ namespace yii\sphinx; * @property QueryBuilder $queryBuilder The query builder for this Sphinx connection. This property is * read-only. * @method Schema getSchema() The schema information for this Sphinx connection - * @method QueryBuilder getQueryBuilder() he query builder for this Sphinx connection + * @method QueryBuilder getQueryBuilder() the query builder for this Sphinx connection * * @author Paul Klimov * @since 2.0 diff --git a/extensions/sphinx/QueryBuilder.php b/extensions/sphinx/QueryBuilder.php index 91e3b62..997e997 100644 --- a/extensions/sphinx/QueryBuilder.php +++ b/extensions/sphinx/QueryBuilder.php @@ -204,9 +204,10 @@ class QueryBuilder extends Object * refer to [[Query::where()]] on how to specify condition. * @param array $params the binding parameters that will be modified by this method * so that they can be bound to the DB command later. + * @param array $options list of options in format: optionName => optionValue * @return string the UPDATE SQL */ - public function update($index, $columns, $condition, &$params) + public function update($index, $columns, $condition, &$params, $options) { if (($indexSchema = $this->db->getIndexSchema($index)) !== null) { $columnSchemas = $indexSchema->columns; @@ -241,7 +242,14 @@ class QueryBuilder extends Object $sql = 'UPDATE ' . $this->db->quoteIndexName($index) . ' SET ' . implode(', ', $lines); $where = $this->buildWhere($condition, $params); - return $where === '' ? $sql : $sql . ' ' . $where; + if ($where !== '') { + $sql = $sql . ' ' . $where; + } + $option = $this->buildOption($options, $params); + if ($option !== '') { + $sql = $sql . ' ' . $option; + } + return $sql; } /** diff --git a/tests/unit/extensions/sphinx/CommandTest.php b/tests/unit/extensions/sphinx/CommandTest.php index af34c4f..5986182 100644 --- a/tests/unit/extensions/sphinx/CommandTest.php +++ b/tests/unit/extensions/sphinx/CommandTest.php @@ -233,6 +233,36 @@ class CommandTest extends SphinxTestCase } /** + * @depends testUpdate + */ + public function testUpdateWithOptions() + { + $db = $this->getConnection(); + + $db->createCommand()->insert('yii2_test_rt_index', [ + 'title' => 'Test title', + 'content' => 'Test content', + 'type_id' => 2, + 'id' => 1, + ])->execute(); + + $newTypeId = 5; + $command = $db->createCommand()->update( + 'yii2_test_rt_index', + [ + 'type_id' => $newTypeId, + 'non_existing_attribute' => 10, + ], + 'id = 1', + [], + [ + 'ignore_nonexistent_columns' => 1 + ] + ); + $this->assertEquals(1, $command->execute(), 'Unable to execute update!'); + } + + /** * @depends testInsert */ public function testDelete()