MV framework logo
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Documentation
Download .zip version 3.2.0 from 25.12.2024
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
Documentation
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
MV tracker

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

Next section

Records management
MV workshop banner
MV tracker

© 2014-2025, MV framework team

MV tracker project Github