Can not establish 2 DB relationships between 2 tables

51 Views Asked by At

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.

  1. A Product can have a single Category. The Product model has public $belongsTo = [ 'category' => Category::class ]; while the Category has public $hasMany = [ 'products' => Product::class ];. And in the Product field.yml file
category:
    label: Category
    type: relation
    span: auto

This relationship works.

  1. I need to make it so for a Category I 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:

  1. A Product can have a single category
  2. A Category can additionally assign any 3 products as a showcase product (separate columns)
0

There are 0 best solutions below