From 0bc91f29e7baa562e9ab06d5a51830dabc1692be Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Sat, 16 Nov 2013 15:32:03 +0200 Subject: [PATCH] Sphinx Active Record relation handling provided. --- extensions/sphinx/ActiveRecord.php | 23 +++++++++++++++++++++++ tests/unit/data/sphinx/ar/ArticleDb.php | 4 ++-- tests/unit/data/sphinx/ar/ItemDb.php | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index 08617b4..690a59d 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -1126,4 +1126,27 @@ class ActiveRecord extends Model $transactions = $this->transactions(); return isset($transactions[$scenario]) && ($transactions[$scenario] & $operation); } + + /** + * Sets the element at the specified offset. + * This method is required by the SPL interface `ArrayAccess`. + * It is implicitly called when you use something like `$model[$offset] = $item;`. + * @param integer $offset the offset to set element + * @param mixed $item the element value + * @throws \Exception on failure + */ + public function offsetSet($offset, $item) + { + // Bypass relation owner restriction to 'yii\db\ActiveRecord' at [[ActiveRelationTrait::findWith()]]: + try { + $relation = $this->getRelation($offset); + if (is_object($relation)) { + $this->populateRelation($offset, $item); + return; + } + } catch (UnknownMethodException $e) { + throw $e->getPrevious(); + } + parent::offsetSet($offset, $item); + } } \ No newline at end of file diff --git a/tests/unit/data/sphinx/ar/ArticleDb.php b/tests/unit/data/sphinx/ar/ArticleDb.php index de9ca0f..3c32f99 100644 --- a/tests/unit/data/sphinx/ar/ArticleDb.php +++ b/tests/unit/data/sphinx/ar/ArticleDb.php @@ -2,9 +2,9 @@ namespace yiiunit\data\sphinx\ar; -use yiiunit\data\ar; +use yiiunit\data\ar\ActiveRecord as ActiveRecordDb; -class ArticleDb extends ActiveRecord +class ArticleDb extends ActiveRecordDb { public static function tableName() { diff --git a/tests/unit/data/sphinx/ar/ItemDb.php b/tests/unit/data/sphinx/ar/ItemDb.php index 128e473..ddf7eea 100644 --- a/tests/unit/data/sphinx/ar/ItemDb.php +++ b/tests/unit/data/sphinx/ar/ItemDb.php @@ -2,9 +2,9 @@ namespace yiiunit\data\sphinx\ar; -use yiiunit\data\ar; +use yiiunit\data\ar\ActiveRecord as ActiveRecordDb; -class ItemDb extends ActiveRecord +class ItemDb extends ActiveRecordDb { public static function tableName() {