thewunder / corma
基于约定的替代ORM
5.0.0
2024-09-16 18:14 UTC
Requires
- php: >=8.1
- ext-json: *
- ext-pdo: *
- doctrine/inflector: ^1.3||^2.0
- psr/container: ^2.0
- psr/event-dispatcher: ^1.0
- psr/simple-cache: ^2.0||^3.0
- thewunder/corma-dbal: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.0
- rector/rector: ^1.0
- symfony/event-dispatcher: ^6.0 || ^7.0
- vlucas/phpdotenv: ~2.0
Suggests
- dev-master
- 5.x-dev
- 5.0.0
- 4.x-dev
- 4.1.0
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0
- 3.x-dev
- 3.6.5
- 3.6.4
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.0
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0
- 3.0-beta3
- 3.0-beta2
- 3.0-beta1
- 2.x-dev
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1
- 2.0.2
- 2.0.1
- 2.0
- 2.0-beta2
- 2.0-beta1
- 1.0.x-dev
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-beta1
- 0.9.0
- 0.8.0
- 0.7.8
- 0.7.7
- 0.7.6
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.1
- 0.1
- dev-5.x-fixes
- dev-integration-test-refactor
- dev-relationship_refactor
- dev-relationship_joins
This package is auto-updated.
Last update: 2024-09-16 18:20:15 UTC
README
Corma是一个基于Doctrine DBAL的高性能、基于约定的ORM。
Corma之所以优秀,是因为
- 没有复杂且难以验证的注解或配置文件
- 促进代码组织的一致性
- 加载和保存一对一、一对多、多对多和多态关系
- 可以单个查询中保存多个对象(使用upsert)
- 易于缓存并避免数据库查询
- 支持软删除
- 在单元工作中轻松处理事务
- 高度可定制
Corma不
- 默认自动加载或懒加载关系
- 进行迁移或代码生成
在MySQL和PostgreSQL上运行。
通过Composer安装
通过命令行
composer.phar require thewunder/corma ^5.0
或将以下内容添加到composer.json中的require部分
"thewunder/corma": "^5.0"
对于PHP版本< 8.1,请使用Corma版本~3.0
基本用法
创建一个DataObject
namespace YourNamespace\Dataobjects; use Corma\Relationship\ManyToMany; use Corma\Relationship\OneToMany; use Corma\Relationship\OneToOne; class YourDataObject { protected $id; //If the property name == column name on the table your_data_objects it will be saved protected $myColumn; protected ?int $otherObjectId = null; #[OneToOne] protected ?OtherObject $otherObject = null; #[OneToMany(AnotherObject::class)] protected ?array $anotherObjects = null; #[ManyToMany(DifferentObject::class, 'your_data_object_different_link_table')] protected ?array $differentObjects = null; //Getters and setters.. }
以及一个Repository(可选)
namespace YourNamespace\Dataobjects\Repository; class YourDataObjectRepository extends ObjectRepository { //Override default behavior and add custom methods... }
创建orm并使用它
$db = DriverManager::getConnection(...); //see Doctrine DBAL docs $orm = ObjectMapper::withDefaults($db, $container); //uses any PSR-11 compatible DI container $object = $orm->create(YourDataObject::class); //Call setters... $orm->save($object); //Call more setters... $orm->save($object); //Call more setters on $object... $objects = [$object]; $newObject = $orm->create(YourDataObject::class); //call setters on $newObject... $objects[] = $newObject; $orm->saveAll($objects); //find existing object by id $existingObject = $orm->find(YourDataObject::class, 5); //find existing objects with myColumn >= 42 AND otherColumn = 1 $existingObjects = $orm->findBy(YourDataObject::class, ['myColumn >='=>42, 'otherColumn'=>1], ['sortColumn'=>'ASC']); //load relationships $orm->load($existingObjects, 'otherObject'); $orm->load($existingObjects, 'anotherObjects'); $orm->load($existingObjects, 'differentObjects'); //delete those $orm->deleteAll($existingObjects);
文档
有关完整文档,请参阅Wiki。
贡献
有关详细信息,请参阅CONTRIBUTING。