Corma is a high-performance, convention-based ORM based on Doctrine DBAL.
Croute is great because:
- No complex and difficult to verify annotations or configuration files
- Promotes consistent code organization
- Allows for customization through symfony events
- Supports soft deletes
- Can save multiple objects in a single query (using an upsert)
- Makes it easy to cache and avoid database queries
- Loads one-to-many and many-to-many relationships with a method call
Corma doesn't:
- Autoload or lazy load anything
- Have any knowledge of relationships between objects
- Have any Unit of Work concept, everything is executed right away
- Do migrations or code generation
Don't use this in production, things will change.
Only tested on MySQL.
Via the command line:
composer.phar require thewunder/corma *
Or add the following to the require section your composer.json:
"thewunder/corma": "*"
Create a DataObject
namespace YourNamespace\Dataobjects;
class YourDataObject extends DataObject {
//If the property name == column name on the table your_data_objects it will be saved
protected $myColumn;
//Getters and setters..
}
And a Repository (optional)
namespace YourNamespace\Dataobjects\Repository;
class YourDataObjectRepository extends DataObjectRepository {
//Override default behavior and add custom methods...
}
Create the orm and use it
$db = DriverManager::getConnection(...); //see Doctrine DBAL docs
$orm = ObjectMapper::create($db, new EventDispatcher(), ['YourNamespace\\Dataobjects']);
$object = $orm->createObject(YourDataObject::class);
//Call setters...
$orm->save($object);
//Call more setters...
$orm->save($object);
//Call more setters on $object...
$objects = [$object];
$newObject = $orm->createObject(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->loadOneToMany($existingObjects, OtherObject::class, 'otherObjectId');
$orm->loadManyToMany($existingObjects, DifferentObject::class, 'link_table');
//delete those
$orm->deleteAll($existingObjects);
Symfony events are dispatched for every stage of the object lifecycle. ObjectName here is the class without namespace.
- DataObject.beforeSave
- DataObject.{ObjectName}.beforeSave
- DataObject.beforeInsert
- DataObject.{ObjectName}.beforeInsert
- DataObject.beforeUpdate
- DataObject.{ObjectName}.beforeUpdate
- DataObject.afterSave
- DataObject.{ObjectName}.afterSave
- DataObject.afterInsert
- DataObject.{ObjectName}.afterInsert
- DataObject.afterUpdate
- DataObject.{ObjectName}.afterUpdate
- DataObject.beforeDelete
- DataObject.{ObjectName}.beforeDelete
- DataObject.afterDelete
- DataObject.{ObjectName}.afterDelete