From bd79e1c1be64d611f88825da29b42670302fd5ff Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Mon, 18 Nov 2013 15:36:06 +0200 Subject: [PATCH] Sphinx Active Record saving via 'replace' converted into fallback --- extensions/sphinx/ActiveRecord.php | 13 +++++++++++++ tests/unit/extensions/sphinx/ActiveRecordTest.php | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index 5256c1c..c275bb4 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -851,7 +851,20 @@ class ActiveRecord extends Model return 0; } + // Replace is supported only by runtime indexes and necessary only for field update + $useReplace = false; + $indexSchema = $this->getIndexSchema(); if ($this->getIndexSchema()->isRuntime) { + foreach ($values as $name => $value) { + $columnSchema = $indexSchema->getColumn($name); + if ($columnSchema->isField) { + $useReplace = true; + break; + } + } + } + + if ($useReplace) { $values = array_merge($values, $this->getOldPrimaryKey(true)); $command = static::getDb()->createCommand(); $command->replace(static::indexName(), $values); diff --git a/tests/unit/extensions/sphinx/ActiveRecordTest.php b/tests/unit/extensions/sphinx/ActiveRecordTest.php index 815ebe8..0bb9dbb 100644 --- a/tests/unit/extensions/sphinx/ActiveRecordTest.php +++ b/tests/unit/extensions/sphinx/ActiveRecordTest.php @@ -161,6 +161,16 @@ class ActiveRecordTest extends SphinxTestCase $record2 = RuntimeIndex::find(['id' => 2]); $this->assertEquals(9, $record2->type_id); + // replace + $query = 'replace'; + $rows = RuntimeIndex::find($query); + $this->assertEmpty($rows); + $record = RuntimeIndex::find(['id' => 2]); + $record->content = 'Test content with ' . $query; + $record->save(); + $rows = RuntimeIndex::find($query); + $this->assertNotEmpty($rows); + // updateAll $pk = ['id' => 2]; $ret = RuntimeIndex::updateAll(['type_id' => 55], $pk);