Mail: [email protected]
Phone: +1(424)231-4091
Everything you need to know about NexoPOS.
The model dependency ensures a model cannot be deleted if it's still used as a reference on another model. For example, a customer cannot be deleted if he's still attached to an order, and a product cannot be deleted if it's still attached to an order product.
This feature tends to protect the integrity of your data by enforcing the proper deletion of entries to avoid orphan and hidden entries.
The dependency is made by default on each model. You'll use the property "setDependencies" where you'll provide an array with the related class as a key and another array to configure the configuration. Here is an example.
Sometimes, you want to link your dependency to another related table. Let's consider the following scenario. You have a recipe that uses products as ingredients. We have here a middleman table that links the products to the recipe ingredient tables. Typically that middleman table doesn't have any "name" attribute, therefore, we'll use the name of the related table.
Here is a detailed structure for this example.
Products
-> name: "Tomato"
-> id: 2
Recipe
->name "Hot Tomato"
->id 1
Ingredient
-> product_id: 2
-> recipe_id: 1
Here, we need to create a way to prevent Products from being deleted if it's used as Ingredient and we need to use the Recipe name to show an error like: "Tomato cannot be deleted as it's a dependency for Hot Tomato".
The error thrown so far pulls the name of the items and displays it as being dependent. However, you might want to give a context to that name. In our previous example, wouldn't it be nice if instead of "Hot Tomato" we could use "Recipe: Hot Tomato"? That's where the "prefix" on the related method is used.
You'll define as named argument "prefix" a callback function that uses the name of the dependant model as parameter. From there, you can return any other localized string using that name.
You're not forced to only provide a dependency for your model, but you can provide a custom dependency to other models. For example, a user cannot be deleted if he still has a book that he entered on the system.
for that, on our module, we'll register that dependency as follows: