MV framework logo
  • Admin Panel
  • Architecture
  • Support
  • Feedback
Download .zip ver. 3.4.2 from 04.03.2026
Dark mode
Download .zip ver. 3.4.2 from 04.03.2026
  • Admin Panel
  • Architecture
  • Support
  • Feedback
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
  • Sessions
  • Authorization
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
MV tracker
Dark mode
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
  • Sessions
  • Authorization
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
MV tracker
Home Models Foreign keys

Foreign key relation

If a model uses an enum field, its list of values ​​can be data from another model. Such a relationship is built on the basis of foreign keys.

An example of a relationship between news and comments, each comment refers to a specific news item and is linked to it by a foreign key.

class News extends Model
{
    protected $name = 'News';
    
    protected $model_elements = [
        ['Activate', 'bool', 'active', ['on_create' => true]],
        ['Title', 'char', 'name', ['required' => true]],
        ['Date', 'date_time', 'date', ['required' => true]],
        ['Content', 'text', 'content', ['rich_text' => true]],
        ['Comments', 'one_to_many', 'comments', ['related_model' => 'Comments']]
    ];
}

class Comments extends Model
{
    protected $name = 'Comments';
    
    protected $model_elements = [
        ['Activate', 'bool', 'active', ['on_create' => true]],
        ['Date', 'date_time', 'date', ['required' => true]],
        ['Name', 'char', 'name', ['required' => true]],
        ['News', 'enum', 'news_id', ['foreign_key' => 'News']],
        ['Contents', 'text', 'content']
    ];
}

The News model has a Comments field of the One to many type, so that you can see the number of comments for each news item, as well as quickly go to the Comments model with filtered records related to this news item.

Displaying records by foreign key

Let's assume that all news items are displayed by the URL /news, and a single news item by a URL like /news/25.

//config/routes.php file

'/news' => 'view-news-all.php',
'/news/*' => 'view-news-detailed.php',
<?
//file views/view-news-detailed.php

$url_part = $mv -> checkUrlPart(2, 'numeric');
$news_record = $mv -> news -> find(['active' => 1, 'id' => $url_part]);
$mv -> display404($news_record);

$mv -> seo -> mergeParams($news_record -> name.' '.I18n::formatDate($news_record -> date));
$total = echo $mv -> comments -> countRecords(['active' => 1, 'news_id' => $news_record -> id]);

include $mv -> views_path.'main-header.php'; 
?>

<main>
    <div class="date"><? echo I18n::formatDate($news_record -> date); ?></div>
    <h1><? echo $news_record -> name; ?></h1>

    <? echo $news_record -> content; ?>

    <div id="comments">
        <h3>Comments: <? echo $total; ?></h3>
        <ul>
            <? echo $mv -> comments -> display($news_record -> id); ?>
        </ul>
    </div>
</main>

<? include $mv -> views_path.'main-footer.php'; ?>

Adding the function to display comments for the Comments model.

<?
class Comments extends Model
{
    ...

    public function display(int $news_id): string
    {
        $html = '';

        $rows = $this -> select([
            'active' => 1,
            'news_id' => $news_id,
            'order->desc' => 'date'
        ]);

        foreach($rows as $row)
        {
            $html .= '<li>';
            $html .= '<span class="date">'.$row['name'].' '.I18n::formatDate($row['date']).'</span>';
            $html .= '<p>'.$row['content'].'</p>';
            $html .= '</li>';
        }

        return $html;
    }      
}

Previous section

Simple models

Next section

Trees
MV workshop banner
MV tracker

© 2014-2026, MV framework team

MV tracker project Github