scienta/doctrine-json-functions
Adds JSON function support to Doctrine ORM DQL by registering custom function nodes for multiple databases. Use MySQL/MariaDB, PostgreSQL, SQLite (json1), or SQL Server JSON functions directly in DQL with platform validation.
where(), orWhere()).addCustomStringFunction or Symfony’s doctrine.yaml). For Laravel, this can be centralized in a service provider or config file.QueryBuilder (e.g., Model::whereJsonContains('column', 'value')).->> operator), but this package provides DQL alternatives.connection('pgsql')) must align with the package’s platform checks to avoid runtime errors.JSON_CONTAINS) require explicit comparison (= 1 or = true), which may need wrapper methods in Laravel repositories to abstract this.JSON_VALUE in MySQL 8.0.21+ or MariaDB-specific functions may break if the underlying database version is unsupported. Mitigation: Validate database versions during deployment (e.g., via migrations or CI checks).jsonb_path_ops).whereJsonExtract()).laravel-doctrine-json) to abstract Doctrine config?User::whereJsonContains('roles', 'admin'))?JSONB_TYPEOF) be adopted?QueryBuilder, enabling JSON queries via DQL. Example:
// MySQL/PostgreSQL
$users = User::whereJsonContains('metadata', '"premium"')
->whereJsonExtract('metadata', '$.tier', '>=' => 3)
->get();
JSON_EXTRACT.JSONB_EXISTS.JSON_EXTRACT, JSON_CONTAINS) with existing queries.config/doctrine.php).// app/Providers/DoctrineServiceProvider.php
public function registerFunctions()
{
$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction(
\Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract::FUNCTION_NAME,
\Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract::class
);
// Repeat for other functions...
}
// app/Models/User.php
public function scopeWhereJsonContains($query, $column, $value)
{
return $query->where("JSON_CONTAINS($column, :value) = 1", ['value' => json_encode($value)]);
}
jsonb type for optimal performance. Test json vs. jsonb behavior.json1 extension (enabled by default in Laravel’s SQLite driver).JSON_VALUE; other functions may need raw SQL.^2.19 or ^3). Risk: Upgrading Doctrine may require re-testing.scienta/doctrine-json-functions and Doctrine ORM.composer.json to avoid breaking changes.JSON_VALUE in MySQL < 8.0.21).= 1 or = true.// app/Helpers/JsonQuery.php
public static function boolJsonFunction(string $function, ...$args): string
{
return "$function(...) = 1";
}
config('database.log_queries' => true)) to inspect generated SQL.How can I help you explore Laravel packages today?