diff --git a/common/config/config.php b/common/config/config.php index 3df1fbb..92f07f9 100644 --- a/common/config/config.php +++ b/common/config/config.php @@ -12,6 +12,9 @@ return [ 'username' => getenv('MYSQL_USER'), 'password' => getenv('MYSQL_PASSWORD'), 'charset' => 'utf8', + 'schemaMap' => [ + 'mysql' => common\db\mysql\Schema::class, + ], ], 'mailer' => [ 'class' => yii\swiftmailer\Mailer::class, diff --git a/common/db/mysql/QueryBuilder.php b/common/db/mysql/QueryBuilder.php new file mode 100644 index 0000000..f467396 --- /dev/null +++ b/common/db/mysql/QueryBuilder.php @@ -0,0 +1,39 @@ + $direction) { + if ($direction instanceof Expression) { + $orders[] = $direction->expression; + } elseif (is_array($direction)) { + // This is new feature + if (empty($direction)) { + continue; + } + + $fieldValues = []; + foreach($direction as $fieldValue) { + $fieldValues[] = $this->db->quoteValue($fieldValue); + } + + $orders[] = 'FIELD(' . $this->db->quoteColumnName($name) . ',' . implode(',', $fieldValues) . ')'; + // End of new feature + } else { + $orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : ''); + } + } + + return 'ORDER BY ' . implode(', ', $orders); + } + +} diff --git a/common/db/mysql/Schema.php b/common/db/mysql/Schema.php new file mode 100644 index 0000000..301d828 --- /dev/null +++ b/common/db/mysql/Schema.php @@ -0,0 +1,12 @@ +db); + } + +} diff --git a/tests/codeception/common/unit/db/mysql/QueryBuilderTest.php b/tests/codeception/common/unit/db/mysql/QueryBuilderTest.php new file mode 100644 index 0000000..77a3d96 --- /dev/null +++ b/tests/codeception/common/unit/db/mysql/QueryBuilderTest.php @@ -0,0 +1,16 @@ +db); + $result = $queryBuilder->buildOrderBy(['dummy' => ['first', 'second']]); + $this->assertEquals("ORDER BY FIELD(`dummy`,'first','second')", $result); + } + +}