Group relation
If it is necessary to link records together within one table, the Group data type is used. For example, products must have related products that are set for each product individually, and their quantity may be different for each product.
class Products extends Model
{
protected $name = 'Catalog products';
protected $model_elements = [
['Active', 'bool', 'active', ['on_create' => true]],
['Name', 'char', 'name', ['required' => true]],
['Price', 'int', 'price', ['required' => true]],
['Position', 'order', 'order'],
['Catalog section', 'enum', 'parent', ['foreign_key' => 'Catalogs',
'is_parent' => true]],
['Image', 'multi_images', 'images'],
['Description', 'text', 'desc', ['rich_text' => true]],
['Additional products', 'group', 'additional']
];
}
When extracting data from the 'additional' field, it is convenient to substitute it into the 'field->in' operator or the 'field->not-in' exclusive operator described in the Query builder section.
//Found the product you needed
$product = $mv -> products -> find(35);
//We extract related products if there are any
if($product -> additional)
{
$rows = $mv -> products -> select(['active' => 1,
'order->asc' => 'order',
'id->in' => $product -> additional]);
foreach($rows as $row)
{
...
}
}
An alternative way to group products is to add a field of the enum type, where each product is selected from a drop-down list by the name of its group.
class Products extends Model
{
protected $name = 'Catalog products';
protected $model_elements = [
...
['Additional products', 'enum', 'additional', ['values_list' => [
'hits' => 'Hits',
'sale' => 'Sale'
]]]
];
}
Previous section
Many to many