-
Notifications
You must be signed in to change notification settings - Fork 4
Object Conventions
Corma is a convention-over-configuration framework, and thus reduces necessary configuration and encourages consistency. All of these conventions can be changed both on a global level, and on an individual object basis.
Corma uses the following default conventions:
- The class of your object (without namespace) is converted to snake case and pluralized to get the table name. For example MyObject becomes "my_objects".
- Your objects / tables use 'id' as their auto-increment primary key. Your object must have a getId and setId method.
- Your object property names must be exactly the same as your database column names.
Repositories must abide by the following rules:
- Your repository must implement ObjectRepositoryInterface, we recommend extending ObjectRepository.
- If your DataObject is MyNamespace\DataObjects\MyObject then your repository must be MyNamespace\DataObjects\Repository\MyObjectRepository
The table name and primary key are easily customizable via attributes.
#[DbTable('custom_table')]
#[IdColumn('custom_id')]
class MyObject
{
//Object properties and methods..
}
Further exceptions to identifier behavior, object creation behavior, and column to property mappings can be achieved by creating a custom ObjectManager in your repository classes.
class MyObjectRepository extends ObjectRepository
{
public function getObjectManager(): ObjectManager
{
if($this->objectManager) {
return $this->objectManager;
}
$objectManagerFactory = $this->objectMapper->getObjectManagerFactory();
$this->objectManager = $objectManagerFactory->getManager($this->getClassName(), $this->objectDependencies /* Custom stuff here! */);
}
}
The object manager factory has several optional parameters that let you customize these conventions:
- $hydrator Setting a custom object hydrator can change how columns are mapped to property names and how those properties are set
- $identifier Setting a custom object identifier can change how your id is generated, retrieved, and set
- If you have enabled annotations (by constructing Corma with an annotation reader), you should use @table instead of injecting a custom convention here.
- $factory Setting a custom object factory will customize how your object is instantiated
To set / modify the convention used by all your objects, you will need to create a custom ObjectManagerFactory instance, and pass it into the ObjectMapper constructor.
//These are all hypothetical custom classes, not part of Corma
$hydrator = new Myhydrator();
$identifier = new GuidIdentifier();
$tableConvention = new NonPluralTableConvention();
$objectFactory = new AutoDepencencyFactory();
$objectManagerFactory = new ObjectManagerFactory($hydrator, $identfier, $tableConvention, $objectFactory);
$objectRepositoryFactory = new ObjectRepositoryFactory();
$cache = new Psr16Cache();
$queryHelper = new MySQLQueryHelper($dbConnection, $cache);
$orm = new ObjectMapper($queryHelper, $objectRepositoryFactory, $objectManagerFactory, new Inflector());
$objectRepositoryFactory->setDependencies([$dbConnection, $orm, $cache]);