butschster/cycle-orm
Composer 安装命令:
composer require butschster/cycle-orm
包简介
Cycle ORM integration for the Laravel Framework
README 文档
README
Cycle is a PHP DataMapper ORM and Data Modelling engine designed to safely work in classic and daemonized PHP applications (like RoadRunner). The ORM provides flexible configuration options to model datasets, a powerful query builder, and supports dynamic mapping schemas. The engine can work with plain PHP objects, support annotation declarations, and proxies via extensions.
Full information - https://cycle-orm.dev/docs
Requirements
- Laravel 7.x
- PHP 7.4 and above
Installation and Configuration
From the command line run
composer require butschster/cycle-orm
Optionally you can register the EntityManager, Transaction and/or ORM facade:
'DatabaseManager' => Butschster\Cycle\Facades\DatabaseManager::class, 'Transaction' => Butschster\Cycle\Facades\Transaction::class, 'ORM' => Butschster\Cycle\Facades\ORM::class, 'EntityManager' => Butschster\Cycle\Facades\EntityManager::class,
Env variables
DB_CONNECTION=postgres
DB_HOST=127.0.0.1
DB_PORT= # Default port 5432
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=
DB_MIGRATIONS_TABLE=migrations # Migrations table name
DB_SCHEMA_SYNC=false # Sync DB schema without migrations
DB_SCHEMA_CACHE=true # Cache DB schema
DB_SCHEMA_CACHE_DRIVER=file # DB schema cache driver
Configuration
Publish the config file.
php artisan vendor:publish --provider="Butschster\Cycle\Providers\LaravelServiceProvider" --tag=config
Configure Databases
The list of available connections and databases can be listed in the config/cycle.php - database section.
For more information see https://cycle-orm.dev/docs/basic-connect#configure-databases
Getting Database Manager ($dbal)
DatabaseManager registered as a singleton container
$dbal = $this->app->get(\Spiral\Database\DatabaseManager::class); // Or $dbal = $this->app->get(\Spiral\Database\DatabaseProviderInterface::class);
That's it!
Console commands
php artisan cycle:migrate
Run cycle orm migrations from the directory.
php artisan cycle:refresh
Refresh database schema.
Usage
By default, class locator looks for entities in app folder. You can specify locations in config/cycle.php config file.
... 'directories' => [ app_path(), ], ...
Entity Manager
The EntityManager is the central access point to ORM functionality. It can be used to find, persist and remove entities.
Using the EntityManager
You can use the facade, container or Dependency injection to access the EntityManager methods
EntityManager::persist($entity); // Or app(\Butschster\Cycle\Contracts\EntityManager::class)->persist($entity); // Or use Butschster\Cycle\Contracts\EntityManager; class ExampleController extends Controller { protected $em; public function __construct(EntityManager $em) { $this->em = $em; } }
Finding entities
Entities are objects with identity. Their identity has a conceptual meaning inside your domain. In a CMS application each article has a unique id. You can uniquely identify each article by that id.
$article = EntityManager::findByPK('App\Article', 1); $article->setTitle('Different title'); $article2 = EntityManager::findByPK('App\Article', 1); if ($article === $article2) { echo "Yes we are the same!"; }
Persisting
By passing the entity through the persist method of the EntityManager, that entity becomes MANAGED, which means that its persistence is from now on managed by an EntityManager.
$article = new Article; $article->setTitle('Let\'s learn about persisting'); EntityManager::persist($article);
Deleting
An entity can be deleted from persistent storage by passing it to the delete($entity) method.
EntityManager::delete($article);
Example
User
<?php namespace App; use Illuminate\Contracts\Auth\Authenticatable; /** * @ORM\Entity( * table="users", * repository="App\UserRepository", * ) */ class User implements Authenticatable { /** @ORM\Column(type="uuid", primary=true) */ protected string $id; /** @ORM\Column(type="string") */ protected string $password; /** @ORM\Column(type="string") */ protected string $rememberToken = ''; public function getId(): string { return $this->id; } /** * @return string */ public function getAuthIdentifierName(): string { return 'id'; } /** * @return string */ public function getAuthIdentifier(): string { return $this->getId(); } /** * @return string */ public function getAuthPassword(): string { return $this->password; } /** * @param string $password */ public function setAuthPassword(string $password): void { $this->password = $password; } /** * @return string */ public function getRememberToken(): string { return $this->rememberToken; } /** * @param string $value */ public function setRememberToken($value): void { $this->rememberToken = $value; } /** * @return string */ public function getRememberTokenName(): string { return 'rememberToken'; } }
Repository
namespace App; use Butschster\Cycle\Repository; class UserRepository extends Repository { /** @inheritDoc */ public function findByUsername(string $username): ?User { return $this->findOne(['username' => $username]); } }
Create user
use Cycle\ORM\ORMInterface; use Butschster\Cycle\Facades\ORM; use Butschster\Cycle\Facades\EntityManager; $user = new User(); $repository = app(ORMInterface::class)->getRepository($user); // or $repository = ORM::getRepository($user); // or $repository = ORM::getRepository(User::class); $repository->persist($user); // or EntityManager::persist($user);
Update user
use Butschster\Cycle\Facades\ORM; use Butschster\Cycle\Facades\EntityManager; $repository = ORM::getRepository(User::class); $user = $repository->findByPK('5c9e177b0a975a6eeccf5960'); $user->setAuthPassword('secret'); $repository->persist($user); // or EntityManager::persist($user);
Delete user
use Butschster\Cycle\Facades\ORM; use Butschster\Cycle\Facades\EntityManager; $repository = ORM::getRepository(User::class); $user = $repository->findByPK('5c9e177b0a975a6eeccf5960'); $repository->delete($user); // or EntityManager::delete($user);
butschster/cycle-orm 适用场景与选型建议
butschster/cycle-orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40 次下载、GitHub Stars 达 36, 最近一次更新时间为 2020 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「laravel」 「cycle」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 butschster/cycle-orm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 butschster/cycle-orm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 butschster/cycle-orm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 40
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 36
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-24
