Skip to main content

Getting Started

Defining the Model

The Model is a class will represent the data that retrieved and want to save into the database.

The Model can be:

  • a simple class with public properties
  • a class with getter and setter
  • a mix of both

Determining the Property Name:

Micro ORM determines the property name based on the following rules:

I'll convert that information into a table for you.

Property VisibilityProperty Name Determination
PublicProperty name is used directly
Protected or PrivateProperty name is determined based on the getter/setter method name

Example:

public ?int $name = null;  // Property name is 'name' 

protected ?int $id = null; // Property name is 'id'

public function getId(): ?int // Getter and setter are necessary.
{
return $this->id;
}
public function setId(?int $id): void
{
$this->id = $id;
}

Requirements for the properties in the Model class:

Property CharacteristicRequirementExample
Property with typeMust be nullable with default value set (null preferred)public ?int $id = null;
Property without typeNo need to set default valuepublic $id;
Protected/Private propertyMust have getter and setter public methodsprotected ?int $id = null;
public function getId(): ?int {...};
public function setId(?int $id): void {...};

Property Mapping Strategy with the Database

StrategyDescriptionExample
Direct MatchingThe ORM matches entity property names to their corresponding database field namesA property named userid would map to a database column named userid
Field MappingThe ORM matches entity property names to their corresponding field mapping database field namesA property named userId would map to a database column named user_id if stated in the field mapping

Field Mapping Methods

MethodDescription
Mapper classDefine mappings using the Mapper class
AttributesDefine mappings using PHP 8.3+ attributes

See also: Controlling the data for advanced mapping

When to use each Mapper or Attribute?

The Mapper class is more flexible and can be used in any PHP version. Use cases:

  • You have a legacy class and cannot change it
  • You don't want change the Model class
  • You can't change the Model class

The Attributes is more simple and can be used in PHP 8.3 or later. Use cases:

  • You want a more simple way to define the Model
  • You want to use the latest PHP features
  • You can have a more simple way to define the Model

Example

Let's say we have a model with the following properties and side by side with the database fields:

Model PropertyDatabase Field
idid
namename
companyIdcompany_id

And we want to map the Model properties to the database fields using Attributes:

#[TableAttribute(tableName: 'mytable')]
class MyModel
{
#[FieldAttribute(primaryKey: true)]
public ?int $id = null;

#[FieldAttribute()]
public ?string $name = null;

#[FieldAttribute(fieldName: 'company_id')]
public ?int $companyId = null;
}

and the table:

CREATE TABLE `mytable`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`company_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In this example, we have a class MyModel with three properties: id, name, and companyId.

  • The id property is marked as a primary key.
  • The name property is a direct match with the database field - No mapping is needed.
  • The companyId property is a field with a different name in the database company_id.
  • The TableAttribute is used to define the table name in the database.

Connect the Model with the repository

After defining the Model, you can connect the Model with the repository.

$dbDriver = Factory::getDbInstance('mysql://user:password@server/schema');
$repository = new Repository($dbDriver, MyModel::class);

Querying the database

You can query the database using the repository.

$myModel = $repository->get(1);

or

$query = Query::getInstance()
->field('name')
->where('company_id = :cid', ['cid' => 1]);

$result = $repository->getByQuery($query);