Related products on table goods

I would like to make similar products to the product.
What should a database look like?

Table Goods

id,
name

Table GoodsRelateds both columns primary key.

good_id,
good_id ???

Column types cannot be named the same, please advise how to deal with this when referring to the same table Goods ?

good_id and related_good_id?

Thank You,

I have form:

$this->Form->control('goods._ids', [
    'label' => 'Similar products',
    'options' => $goods_relateds,
    'multiple' => true,
]);

In Goods table:

$this->hasMany('GoodsRelateds', [
    'foreignKey' => 'good_id',
]);
$this->belongsToMany('Goods', [
    'foreignKey' => 'good_id',
    'targetForeignKey' => 'related_good_id',
    'through' => 'GoodsRelateds',
]);

After save get error:

The Goods association on Goods cannot target the same table.

Please help

I created alias and its works:

$this->belongsToMany('Relateds', [
    'className' => 'Goods',
    'foreignKey' => 'related_good_id',
    'targetForeignKey' => 'good_id',
    'through' => 'GoodsRelateds',
]);
2 Likes