diff --git a/src/foreignField/ForeignFieldQueryExtension.php b/src/foreignField/ForeignFieldQueryExtension.php index 4d3142a..1f433ad 100644 --- a/src/foreignField/ForeignFieldQueryExtension.php +++ b/src/foreignField/ForeignFieldQueryExtension.php @@ -8,6 +8,7 @@ use craft\elements\db\ElementQueryInterface; use craft\helpers\ArrayHelper; use craft\helpers\Db; +use craft\helpers\Json; use Exception; use Yii; @@ -41,6 +42,16 @@ class ForeignFieldQueryExtension */ public $query; + /** + * @var bool + */ + private $_didAttachEagerLoad = false; + + /** + * @var bool + */ + private $_didAttachJoin = false; + /** * ForeignFieldQueryExtension constructor. @@ -81,6 +92,9 @@ public function onAfterPrepare() { * @return void */ protected function attachEagerLoad() { + if ($this->_didAttachEagerLoad) return; + $this->_didAttachEagerLoad = true; + $this->query->query->addSelect([ 'field:' . $this->field->handle => $this->getJsonExpression(), ]); @@ -90,6 +104,9 @@ protected function attachEagerLoad() { * @return void */ protected function attachJoin() { + if ($this->_didAttachJoin) return; + $this->_didAttachJoin = true; + /** @var ActiveRecord $recordClass */ $recordClass = $this->field::recordClass(); $tableName = $recordClass::tableName(); @@ -219,7 +236,8 @@ static protected function enableEagerLoad(ElementQuery $query, ForeignField $fie static protected function enableJoin(ElementQuery $query, ForeignField $field) { $items = array_merge( is_array($query->orderBy) ? $query->orderBy : [(string)$query->orderBy], - is_array($query->groupBy) ? $query->groupBy : [(string)$query->groupBy] + is_array($query->groupBy) ? $query->groupBy : [(string)$query->groupBy], + [Json::encode($query->where)] ); foreach ($items as $item) {