I have an October cms 3.x app.
I have 2 models (tables) Product and Category. I need to establish 2 relationships between them. A Product should have 1 Category assigned to it. and a Category additionally should be able to be assigned 3 different Product as a showcase product.
- A
Productcan have a single Category. TheProductmodel haspublic $belongsTo = [ 'category' => Category::class ];while theCategoryhaspublic $hasMany = [ 'products' => Product::class ];. And in theProductfield.yml file
category:
label: Category
type: relation
span: auto
This relationship works.
- I need to make it so for a
CategoryI could assign specific products as a showcase products (used to display in the category page). And this is the problematic part. If i add
showcases:
label: Showcase product
type: relation
span: auto
to the category field.yml and in the Category model I set
public $hasMany = [
'products' => Product::class,
'showcases' => Product::class
];
what ends up happening is it displays products that have this specific category selected. And if I edit it, it just changed the product category rather than saving this as a showcase product (new column).
I also tried
public $hasMany = [
'products' => Product::class,
'showcases' => [
Product::class,
'table' => 'category_showcase_products',
'key' => 'category_id',
'otherKey' => 'product_id'
],
];
by creating a category_showcase_products table that has just 2 columns category_id and product_id. Still same behavior.
So what should be happening is:
- A
Productcan have a single category - A
Categorycan additionally assign any 3 products as a showcase product (separate columns)