Hi
It is good to have a method that contains the schema of the model.
Based on this method, it is easy to create migration, connect to different types of databases(such as Mysql, mongoDb or …) and have hierarchical structure in the model.
for example :
Instead of ‘attributeLabels’ and ‘rules’ methods, we have the following method in ActiveRecord or Model class:
public function attributes()
{
return [
'id' => [
'label' => 'ID',
'type' => 'bigIncrement',
'default' => null,
],
'user_id' => [
'label' => 'User ID',
'type' => 'bigint',
//We can mention relationships here or not
],
'title' => [
'label' => 'Title',
'type' => 'text',
'rules' => ['min' => 5, 'max' => 48, 'required' => true],
'default' => '',
],
'customer_code' => [
'label' => 'Customer Code',
'type' => 'int',
'rules' => function ($value) {
return preg_match('/anything/', $value);
},
],
'metadata' => [
'type' => 'json',
'structure' => [
'author_name' => [
'label' => 'author name',
'type' => 'text',
],
'author_family' => [
'label' => 'author family',
'type' => 'text',
],
'author_gender' => [
'label' => 'author gender',
'type' => 'text',// or enum
'option' => [
'u' => 'Unknown',
'm' => 'Man',
'w' => 'Woman',
],
],
],
],
'status' => [
'label' => 'Status',
'type' => 'int',// or enum
'option' => [
0 => 'Draft',
1 => 'Pending',
2 => 'Accepted',
],
],
'created_at' => 'createTimestamp',//using template
'updated_at' => 'updateTimestamp',//using template
];
}
With this method, the problem of matching different databases will be largely solved, and we can handle the CRUD with a general methods based on this schema.