🎲 This project provides an integration for the Doctrine ORM and the Hyperf framework

Overview

Hyperf 🤝 Doctrine

This project provides an integration for the Doctrine ORM and the Hyperf framework.

CI codecov PHPStan

Install

composer require leocavalcante/hyperf-doctrine

Setup

You should publish hyperf/db as this package uses it as the underlaying driver engine:

php bin/hyperf.php vendor:publish hyperf/db

This means that you have Doctrine ORM baked into Hyperf pools and connections lifecycle management free of charge - How awesome?

Configure the database in config/autoload/db.php:

return [
    'default' => [
        'driver' => 'pdo',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', 3306),
        'database' => env('DB_DATABASE', 'hyperf'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => env('DB_CHARSET', 'utf8mb4'),
        'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
        'fetch_mode' => PDO::FETCH_ASSOC,
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 10,
            'connect_timeout' => 10.0,
            'wait_timeout' => 3.0,
            'heartbeat' => -1,
            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
        ],
        'options' => [
            PDO::ATTR_CASE => PDO::CASE_NATURAL,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
            PDO::ATTR_STRINGIFY_FETCHES => false,
            PDO::ATTR_EMULATE_PREPARES => false,
        ],
    ],
];

Then publish Hyperf Doctrine configurations:

php bin/hyperf.php vendor:publish leocavalcante/hyperf-doctrine

You can edit Doctrine's Entity Manager settings in config/autoload/doctrine.php:

return [
    'connection' => [
        'driverClass' => Hyperf\Doctrine\Driver::class,
        'pool' => 'default',
    ],
    'config' => Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration([__DIR__ . '/app']),
];

It will also publish a cli-config.php to config/ so you can already run vendor/bin/doctrine, for example:

vendor/bin/doctrine orm:schema-tool:create

Usage

At this time, you are ready to use Doctrine ORM with Hyperf.

For example, create an Entity as you would do regulary:

/**
 * @ORM\Entity()
 * @ORM\Table(name="users")
 */
final class User
{
    public function __construct(
        /**
         * @ORM\Id()
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        public int $id,
        /*
         * @ORM\Column(type="string")
         */
        public string $name,
        /**
         * @ORM\Column(type="string")
         */
        public string $email,
    ) {
    }
}

Then let dependency injection magic do the work to inject an EntityManager into your application:

em->getRepository(User::class)->findAll(); } }">
/**
 * @Controller(prefix="users")
 */
final class UsersController
{
    public function __construct(
        private EntityManagerInterface $em,
    ) {
    }

    /**
     * @GetMapping(path="")
     */
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        return $this->em->getRepository(User::class)->findAll();
    }
}

And that is it!

Feel free to contribute submiting issues and PRs.


MIT © 2022 Leo Cavalcante

You might also like...
Plates Template Integration for Slim micro framework 3

Plates Template Integration for Slim micro framework 3 Render your Slim 3 application views using Plates template engine. Install Via Composer $ compo

Integration testing helpers for the Slim Framework

Slim Test Helpers Integration testing helpers for the Slim Framework 3 For a full example, please see the companion repo at there4/slim-unit-testing-e

Kit is a lightweight, high-performance and event-driven web services framework that provides core components such as config, container, http, log and route.

Kit What is it Kit is a lightweight, high-performance and event-driven web services framework that provides core components such as config, container,

QPM, the process management framework in PHP, the efficient toolkit for CLI development. QPM provides basic daemon functions and supervision mechanisms to simplify multi-process app dev.

QPM QPM全名是 Quick(or Q's) Process Management Framework for PHP. PHP 是强大的web开发语言,以至于大家常常忘记PHP 可以用来开发健壮的命令行(CLI)程序以至于daemon程序。 而编写daemon程序免不了与各种进程管理打交道。Q

This extension provides a view renderer for Pug templates for Yii framework 2.0 applications.

This extension provides a view renderer for Pug templates for Yii framework 2.0 applications.

a framework for WebDevelop based on the mvc structure. The name of this project for Fun because everyone can use it. Completely simple and powerful structure for all your projects

A_A (-.-) ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ |-| █▄─▄▄─█▄─██─▄█─▄▄▄▄█─▄▄▄▄█▄─█─▄█─▄▄▄─██▀▄─██─▄

Easy integration of twitters bootstrap into symfony2

MopaBootstrapBundle MopaBootstrapBundle is a collection of code to integrate twitter's bootstrap (http://twitter.github.com/bootstrap/) as easy as pos

🎁  Datagrid component project skeleton based on Nette Framework
🎁 Datagrid component project skeleton based on Nette Framework

🎁 Datagrid component project skeleton based on Nette Framework

Comments
  • Error when I try to create a schema

    Error when I try to create a schema

    /opt/www # vendor/bin/doctrine orm:schema-tool:create sf PHP Fatal error: Uncaught RuntimeException: Setup tool cannot configure caches without doctrine/cache 1.11 or symfony/cache. Please add an explicit dependency to either library. in /opt/www/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Setup.php:193

    wontfix 
    opened by Ananiaslitz 1
Releases(v0.1.0)
Owner
Leo Cavalcante
@PicPay Software Engineer 👨‍💻 @PHP Evangelist 🐘 @Swoole Project Member 🚀 @Hyperf Enthusiast 💟 @PHPSP Community Advocate 👥
Leo Cavalcante
Hyperf is an extremely performant and flexible PHP CLI framework

?? A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.

Hyperf 5k Jan 4, 2023
Hyperf instant messaging program based on swoole framework

Hyperf instant messaging program based on swoole framework

null 20 Aug 12, 2022
Jin microservices is a complete microservice demo based on PHP language + hyperf microservices framework

介绍 Jin-microservices是基于 php 语言 + hyperf 微服务 框架的完整微服务demo。 github:https://github.com/Double-Jin/jin-microservices gitee:https://gitee.com/ljj96/jin-mic

null 114 Dec 29, 2022
An issue tracking tool based on hyperf+reactjs for small and medium-sized enterprises, open-source and free, similar to Jira.

介绍 本项目以 actionview 为蓝本,使用 Hyperf 框架进行重写。 本项目为 Hyperf 框架的 DEMO 项目 原 ActionView 介绍 English | 中文 一个类Jira的问题需求跟踪工具,前端基于reactjs+redux、后端基于php laravel-frame

Gemini-D 14 Nov 15, 2022
🔭 Proof of concept on adding observability features (tracing and metrics) to a Nano microservice (using existing Hyperf components).

?? Tracing Nano Proof of concept on adding observability features (tracing and metrics) to a Nano microservice (using existing Hyperf components ?? ).

Leo Cavalcante 4 Oct 24, 2022
🚀 Developing Rocketseat's Next Level Week (NLW#05) Application using PHP/Swoole + Hyperf

Inmana PHP ?? Developing Rocketseat 's Next Level Week (NLW#05) Application using Swoole + Hyperf. This is the app of the Elixir track. I know PHP/Swo

Leo Cavalcante 18 Jun 1, 2022
This extension provides Flysystem integration for the Yii framework

This extension provides Flysystem integration for the Yii framework. Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.

Alexander Kochetov 276 Dec 9, 2022
Juliangut Slim Framework Doctrine handler middleware

Juliangut Slim Framework Doctrine handler middleware Doctrine handler middleware for Slim Framework. Slim3 version Doctrine integration service for Sl

Julián Gutiérrez 6 Mar 23, 2021
Software for an e-book library, in Bulgarian only. Based on Symfony3 and Doctrine 2.

chitanka.info core Това е уеб софтуер, който задвижва „Моята библиотека“. Изграден е с помощта на Symfony3, Doctrine 2 и много други прекрасни свободн

null 46 Oct 1, 2022
Simple, fast and secure PHP Framework with easy integration.

simple-php-framework Simple, fast and secure PHP Framework with easy integration.

winact 2 Nov 23, 2021