Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thewunder/corma
Convention-based Object-Relational Mapper
https://github.com/thewunder/corma
database orm php
Last synced: 18 days ago
JSON representation
Convention-based Object-Relational Mapper
- Host: GitHub
- URL: https://github.com/thewunder/corma
- Owner: thewunder
- License: mit
- Created: 2016-02-29T22:01:35.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T18:20:12.000Z (about 2 months ago)
- Last Synced: 2024-10-12T18:44:52.650Z (about 1 month ago)
- Topics: database, orm, php
- Language: PHP
- Size: 747 KB
- Stars: 32
- Watchers: 4
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Corma
=====[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.txt)Corma is a high-performance, convention-based ORM based on Doctrine DBAL.
Corma is great because:
* No complex and difficult to verify annotations or configuration files
* Promotes consistent code organization
* Loads and saves one-to-one, one-to-many, many-to-many, and polymorphic relationships
* Can save multiple objects in a single query (using an upsert)
* Makes it easy to cache and avoid database queries
* Supports soft deletes
* Makes it easy to handle transactions in a Unit of Work
* Highly customizableCorma doesn't:
* Autoload or lazy load relationships by default
* Do migrations or code generationWorks in MySql and PostgreSQL.
Install via Composer
--------------------Via the command line:
composer.phar require thewunder/corma ^5.0
Or add the following to the require section your composer.json:
"thewunder/corma": "^5.0"
For PHP versions < 8.1 use Corma version ~3.0
Basic Usage
-----------
Create a DataObject```php
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..
}
```And a Repository (optional)
```php
namespace YourNamespace\Dataobjects\Repository;class YourDataObjectRepository extends ObjectRepository {
//Override default behavior and add custom methods...
}
```Create the orm and use it
```php
$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);
```Documentation
-------------See [the wiki](https://github.com/thewunder/corma/wiki) for full documentation.
Contributing
------------Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
[ico-version]: https://img.shields.io/packagist/v/thewunder/corma.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[link-packagist]: https://packagist.org/packages/thewunder/corma