Home
NexoPOS

Models Dependency

The model's dependency has been introduced on NexoPOS 4.7. x, and that mainly helps to prevent resource deletion that is required by other models. Technically, this means that before deleting a category, NexoPOS will check if there is a product assigned to that category to prevent the deletion.

This guide explains how the model dependency works on NexoPOS.

Declaring Dependency

This should be made on the model itself. We state in the model that another model has a dependency. A model might be dependent on many other models. Every dependency should be clearly defined to prevent deletion.

To declare a dependency, we need to declare a protected property named "isDependencyFor" like this.


<?php
namespace App\Models\Category;


class Category extends NsModel
{
    protected $isDependencyFor  = [
        // dependencies goes here
    ]
}

That property accepts as key the model class, which depends on the current model and the configuration as the value.

<?php
namespace App\Models\Category;


class Category extends NsModel
{
    protected $isDependencyFor  = [
        Product::class  =>  [
            // define configuration
        ]
    ];
}

Here are the supported values for each dependency:

  • local_name: Actual attribute to pull the Model name. That will be used for throwing an understandable error.
  • local_index: Is the actual attribute that is used to compare on dependent models. Usually the value is "id".
  • foreign_name: Attribute of the found dependency to pull it's name. That will also be used to thrown an understandable error.
  • foreign_index: That is the attribute that match the local index, used for identifying dependency.

As our model is "Category",  we declared that to be a dependency for "Product". Here is the final form of our configuration.

<?php
namespace App\Models\Category;


class Category extends NsModel
{
    protected $isDependencyFor  = [
        Product::class  =>  [
            'local_name'  =>  'name',
            'local_index' =>  'id',
            'foreign_name'  =>  'name',
            'foreign_index' =>  'category_id'
        ]
    ];
}

While trying to delete, you'll end up with the following error.

In case a dependency has been found.