This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Slasher
Slasher

I have two models:

  • Brands
  • Mods

I can display all brands via belongsTo function. Problem is when I try to save same brand two times, I get error duplicated entry.

This belongsTo is causing duplicated entry error when saving to DB two same brands.

Mods.php

public $belongsTo = [  
  'brand' => [\Slasher\Farming\Models\Brands::class, 'key' => 'id'],    
  ];

This belongsToMany works, and save's data to DB, but is generating checkbox field (I only want to select one brand at one mod enty). I'm using pivot table for this relation.

Mods.php

public $belongsToMany =[  
    'brand' =>[  
        'Slasher\Farming\Models\Brands',  
        'table' => 'slasher_farming_mods_brands',  
        'order' => 'brand_name'  
    ],  
];

BelongsTo example: (Brands are visible and I can save them. But problem is when saving same brand for more than two times). enter image description here Error I get when saving with belongsTo. enter image description here

I tried also creating inverse relationships on Brands model with (belongsTo and belongsToMany), but still getting this error.

What type of relation should I make, to save Brands as dropdown list and to fix this duplicate error?

oskar.villani40843
oskar.villani40843

Hi,

Not sure understanding your problem completely:

Brands to Mods is a 1:n relation? One brand is able to have several mods. So this is the way I solved this in a similar 1:n relation, I translated it into your model scheme:

Brands requires a "hasMany" relation in the /models/brand.php:

public $hasMany = [
     'brand' => ['Slasher\Farming\Models\Mod', 'key' => 'brand_id']
];

where brand_id is a field in the mods table, Thus you can render a view - as well as to add, link, unlink, delete them - to all mods of a brand (in the form view) e.g. with a partial.

While Mods requires a "belongsTo" in the /models/mod.php relation, like:

public $belongsTo = [
    'mod' => ['Slasher\Farming\Models\Brand', 'key' => 'brand_id']
];    

In the form view you can use a relation widget (dropdown) to select exactly one brand. The funny thing is, that in both relations the key is the same. Anyhow it's working :)

BTW it's a good idea to name models in singular (brand, mod) and controllers as well as tables in plural (brands, mods).

Hope this helps a little,

Best regards


If you have a problem, try to solve it. If you can't solve it, don't make it a problem.

Last updated

Slasher
Slasher

oskar.villani40843 said:

Hi,

Not sure understanding your problem completely:

Brands to Mods is a 1:n relation? One brand is able to have several mods. So this is the way I solved this in a similar 1:n relation, I translated it into your model scheme:

Brands requires a "hasMany" relation in the /models/brand.php:

public $hasMany = [ 'brand' => ['Slasher\Farming\Models\Mod', 'key' => 'brand_id'] ];

where brand_id is a field in the mods table, Thus you can render a view - as well as to add, link, unlink, delete them - to all mods of a brand (in the form view) e.g. with a partial.

While Mods requires a "belongsTo" in the /models/mod.php relation, like:

public $belongsTo = [ 'mod' => ['Slasher\Farming\Models\Brand', 'key' => 'brand_id'] ];

In the form view you can use a relation widget (dropdown) to select exactly one brand. The funny thing is, that in both relations the key is the same. Anyhow it's working :)

BTW it's a good idea to name models in singular (brand, mod) and controllers as well as tables in plural (brands, mods).

Hope this helps a little,

Best regards


If you have a problem, try to solve it. If you can't solve it, don't make it a problem.

Thanks, adding brand_id to mods table, solved this problem.

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.