From d6277fabac5906944de4aafc5215b1b70e45baef Mon Sep 17 00:00:00 2001
From: brandonkelly <brandon@pixelandtonic.com>
Date: Wed, 7 Feb 2024 12:09:20 -0800
Subject: [PATCH] Fixed SQL error

---
 CHANGELOG.md  | 4 ++++
 src/Field.php | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index aefdf258..5c72f1a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Release Notes for CKEditor for Craft CMS
 
+## 4.0.0-beta.2 - 2024-02-07
+
+- Fixed a SQL error that occurred when creating a nested entry on PostgreSQL.
+
 ## 4.0.0-beta.1 - 2024-02-06
 
 - CKEditor now requires Craft CMS 5.0.0-beta.1 or later.
diff --git a/src/Field.php b/src/Field.php
index faeeb409..56ed4ea7 100644
--- a/src/Field.php
+++ b/src/Field.php
@@ -18,6 +18,7 @@
 use craft\ckeditor\events\ModifyConfigEvent;
 use craft\ckeditor\web\assets\BaseCkeditorPackageAsset;
 use craft\ckeditor\web\assets\ckeditor\CkeditorAsset;
+use craft\db\FixedOrderExpression;
 use craft\db\Query;
 use craft\db\Table;
 use craft\elements\Asset;
@@ -54,7 +55,6 @@
 use yii\base\InvalidArgumentException;
 use yii\base\InvalidConfigException;
 use yii\db\Exception;
-use yii\db\Expression;
 
 /**
  * CKEditor field type
@@ -956,7 +956,7 @@ private function _entryManagerValueGetter(): Closure
             $query = $this->createEntryQuery($owner);
             $query->where(['in', 'elements.id', $entryIds]);
             if (!empty($entryIds)) {
-                $query->orderBy(new Expression('FIELD (elements.id, ' . implode(', ', $entryIds) . ')'));
+                $query->orderBy(new FixedOrderExpression('elements.id', $entryIds, Craft::$app->getDb()));
             }
 
             return $query;