PHPackages                             sam-it/yii2-mariadb - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sam-it/yii2-mariadb

ActiveYii2-extension[Database &amp; ORM](/categories/database)

sam-it/yii2-mariadb
===================

MariaDB Driver for Yii2

v4.1.1(8mo ago)30102.9k—9.7%82MITPHPPHP &gt;= 8.4CI failing

Since Jul 13Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/SAM-IT/yii2-mariadb)[ Packagist](https://packagist.org/packages/sam-it/yii2-mariadb)[ RSS](/packages/sam-it-yii2-mariadb/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (4)Versions (19)Used By (2)

[![Latest Stable Version](https://camo.githubusercontent.com/3f2948325b932c6a2b39ccb4fbcadb6eecf972af7ce0bd76f2e8bfffaa9ce470/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f53414d2d49542f796969322d6d6172696164622e737667)](https://packagist.org/packages/sam-it/yii2-mariadb)[![Total Downloads](https://camo.githubusercontent.com/b7d5d7e195d57093e34af3cac99e96cffdfef7b7b7b4dabb7eb834ee1ae13aaf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f53414d2d49542f796969322d6d6172696164622e737667)](https://packagist.org/sam-it/yii2-mariadb)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b65cd7cba5b339781f03452fc7fd065b3c26c85e4796807aed66c13746c9ef71/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f53414d2d49542f796969322d6d6172696164622f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SAM-IT/yii2-mariadb/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/eddbd95bfefebe9e99b84f9eb3dcec6c947e80995348dfba2bb2e634b49661d2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f53414d2d49542f796969322d6d6172696164622f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SAM-IT/yii2-mariadb/?branch=master)[![Continous integration](https://github.com/SAM-IT/yii2-mariadb/actions/workflows/ci.yaml/badge.svg)](https://github.com/SAM-IT/yii2-mariadb/actions/workflows/ci.yaml)

yii2-mariadb
============

[](#yii2-mariadb)

While Yii2 supports MariaDB through its MySQL driver, the differences between MariaDB and MySQL are increasing. At this time the driver included in Yii2 will not properly detect `JSON` columns in MariaDB and will not properly store data in them.

The goal of this library is to implement the MariaDB specific changes required to get all features working in MariaDB that are supported in the Yii2 core library for other DBMSes.

Tests
=====

[](#tests)

The tests coverage is really high due to 2 reasons:

- All code extends their MySQL counter parts in the framework, only very little is added.
- We run the core tests for Yii2 (with some minor changes) to guarantee interoperability with the framework.

Usage
=====

[](#usage)

To use the MariaDB `Schema` implementation there are several approaches.

Override the schema class used for MySQL
----------------------------------------

[](#override-the-schema-class-used-for-mysql)

Update the `schemaMap` property in your `Connection` config (the drivername is still `mysql` since we use the MySQL PDO driver) (RECOMMENDED)

```
'db' => [
    'class' => Connection::class,
    'schemaMap' => [
        'mysql' => SamIT\Yii2\MariaDb\Schema::class
    ]
]
```

Add schema class to schemaMap
-----------------------------

[](#add-schema-class-to-schemamap)

Append the new Schema class to the `schemaMap` property and set the `driverName` property manually.

```
'db' => [
    'class' => Connection::class,
    'driverName' => 'mariadb',
    'schemaMap' => [
        'mariadb' => SamIT\Yii2\MariaDb\Schema::class
    ]
]
```

Modify check condition for the json column
------------------------------------------

[](#modify-check-condition-for-the-json-column)

Update the `columnBuilderSchemaClass` property to modify the check-pattern. Keep in mind: pattern must contain `json_valid` (see section [JSON Column detection](#json-column-detection) )

```
'db' => [
    'class' => Connection::class,
    'schemaMap' => [
        'mysql' => [
            'class' => SamIT\Yii2\MariaDb\Schema::class,
            'columnBuilderSchemaClass' => [
                'class' => SamIT\Yii2\MariaDb\ColumnSchemaBuilder::class,
                'checkPattern' => '[[{name}]] is null or json_valid([[{name}]])',
            ],
        ]
    ]
]
```

JSON Column detection
=====================

[](#json-column-detection)

Since MariaDB has no built-in JSON data type we need to do some extra work to detect JSON columns. We do this by parsing the SQL obtained when using `SHOW CREATE TABLE`. Since MariaDB supports `CHECK` constraints these are used to ensure a column can only contain valid JSON. Any constraint that of the form: `json_valid(`column1`)` will identify the column as JSON. Note that this could lead to problems if you have weird constraints, consider this:

```
`column1` longtext CHECK(not json_valid(`column1`));
```

Will mark `column1` as a JSON column.

Column creation
===============

[](#column-creation)

When creating JSON columns the `ColumnSchemaBuilder` requires the name of the column to add the table constraint. Since this is not the case for all other column types Yii does not pass the name of the column to the builder. Consider this code, for example in a migration:

```
$this->alterColumn('{{test}}', 'field1', $this->json());
```

Here there is no way for the `ColumnSchemaBuilder` to know what the name of the column is going to be. Since the schema builder is ultimately passed to `QueryBuilder::alterColumn()`, we can intercept it there and replace the column name in the constraint.

If you coerce the `ColumnSchemaBuilder` to string early, or use it without the `QueryBuilder` you will end up with SQL like this:

```
ALTER COLUMN `field` JSON CHECK(json_valid({name}));
```

That will clearly not work. For those cases we have added a `toString(string $columnName)` method to the builder.

```
// Will result in broken SQL.
$this->alterColumn('{{test}}', 'field1', $this->json() . ' --APPEND SOMETHING');
// Will result in working SQL.
$this->alterColumn('{{test}}', 'field1', $this->json()->toString('field1') . ' --APPEND SOMETHING');
```

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance59

Moderate activity, may be stable

Popularity43

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity89

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 90.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~155 days

Recently: every ~304 days

Total

18

Last Release

264d ago

Major Versions

v0.1.0 → v1.0.02019-02-13

v1.2.0 → v2.0.02020-05-07

v2.0.0 → v3.0.02020-11-16

v3.1.1 → v4.0.02025-09-01

PHP version history (2 changes)v3.0.0PHP &gt;= 7.4

v4.0.0PHP &gt;= 8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/18b13c534e3812b66a72645fe215301b54fc4d288f6396fee9385b681e27da18?d=identicon)[SamMousa](/maintainers/SamMousa)

---

Top Contributors

[![SamMousa](https://avatars.githubusercontent.com/u/547021?v=4)](https://github.com/SamMousa "SamMousa (103 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![Radon8472](https://avatars.githubusercontent.com/u/30496382?v=4)](https://github.com/Radon8472 "Radon8472 (4 commits)")[![seisvalt](https://avatars.githubusercontent.com/u/295823?v=4)](https://github.com/seisvalt "seisvalt (1 commits)")

---

Tags

mariadbyii2

###  Code Quality

TestsPHPUnit

Code StyleECS

### Embed Badge

![Health badge](/badges/sam-it-yii2-mariadb/health.svg)

```
[![Health](https://phpackages.com/badges/sam-it-yii2-mariadb/health.svg)](https://phpackages.com/packages/sam-it-yii2-mariadb)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[bizley/migration

Migration generator for Yii 2.

296389.5k11](/packages/bizley-migration)[yii2tech/illuminate

Yii2 to Laravel Migration Package

11416.2k](/packages/yii2tech-illuminate)[mootensai/yii2-relation-trait

Yii 2 Models load with relation, &amp; transaction save with relation

49232.7k9](/packages/mootensai-yii2-relation-trait)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
