Skip to content

Object Conventions

Michael O'Connell edited this page May 25, 2022 · 4 revisions

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:

  1. 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".
  2. Your objects / tables use 'id' as their auto-increment primary key. Your object must have a getId and setId method.
  3. Your object property names must be exactly the same as your database column names.

Repositories must abide by the following rules:

  1. Your repository must implement ObjectRepositoryInterface, we recommend extending ObjectRepository.
  2. If your DataObject is MyNamespace\DataObjects\MyObject then your repository must be MyNamespace\DataObjects\Repository\MyObjectRepository

Customizing an Object

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:

  1. $hydrator Setting a custom object hydrator can change how columns are mapped to property names and how those properties are set
  2. $identifier Setting a custom object identifier can change how your id is generated, retrieved, and set
  3. If you have enabled annotations (by constructing Corma with an annotation reader), you should use @table instead of injecting a custom convention here.
  4. $factory Setting a custom object factory will customize how your object is instantiated

Customizing the Defaults

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]);