# Dynamics relations for comments and others generic tables

**URL:** <https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333>\
**Category:** Need Help\
**Tags:** help\
**Created:** [December 9, 2024, 8:02am UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333 "2024-12-09T08:02:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erwane](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/erwane/32/3253_2.png) [@Erwane](https://discourse.cakephp.org/u/Erwane)\
**Post date:** [December 9, 2024, 8:02am UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/1 "2024-12-09T08:02:42Z")

</div>

Hi,  
To link generic tables together, I’m used to using this type of relations.  
By `generic tables` understand : comments, statistics, issues, etc.

```sql
create table comments
(
    id int not null primary key,
    ref varchar(32) not null primary key,
    ref_id int not null primary key,
    content text not null,
);

add index `ref` (`ref`, `ref_id`)

create table articles
(
    id int not null primary key,
    content text not null,
);

create table products
(
    id int not null primary key,
    name varchar(128) not null,
);

```

The hasMany relation is done with `ref` and `ref_id`. The `ref` field refer to model name.

Some websites said : this is a lack of performance, not a good practices, …  
They recommends this style of relations:

```sql
create table comments
(
    id int not null primary key,
    content text not null,
);

create table articles
(
    id int not null primary key,
    content text not null,
);

create table article_comments
(
    article_id int not null,
    comment_id int not null,
);

create table products
(
    id int not null primary key,
    name varchar(128) not null,
);

create table product_comments
(
    product_id int not null,
    comment_id int not null,
);

```

This, required belongsToMany relations.

What do you think about that please ?  
Thanks.

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [December 10, 2024, 5:28pm UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/2 "2024-12-10T17:28:25Z")

</div>

Your use of tables with `ref` and `ref_id` is perfectly fine. That’s a common way of doing polymorphic associations. Just index the 2 columns properly and you are golden.

The alternative should be used only when you in fact need many to many association. But if you just need one to many then the 1st way is totally fine.

---

<div class="post-metadata">

**Author:** ![Erwane](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/erwane/32/3253_2.png) [@Erwane](https://discourse.cakephp.org/u/Erwane)\
**Post date:** [December 10, 2024, 6:53pm UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/3 "2024-12-10T18:53:33Z")

</div>

Thanks, `Polymorphic` was the term i forgot. This kinds or relations are efficient.

I saw some videos about polymorphism in Laravel, they have specials methods for that

> **[Laravel - The PHP Framework For Web Artisans](https://laravel.com/docs/11.x/eloquent-relationships#polymorphic-relationships)**
>
> Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.

And this video is a good example of reusable Trait to configure relations (hasTags, hasComments, …)

[![](https://img.youtube.com/vi/EjJaNGW7vAg/maxresdefault.jpg "Laravel.io Portal: Polymorphic Relations Example") ](https://www.youtube.com/watch?v=EjJaNGW7vAg)

I’ve not said CakePHP should copy Laravel, maybe it’s a good way or overkill.

Thanks @ADmad

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [December 11, 2024, 5:58am UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/4 "2024-12-11T05:58:35Z")

</div>

Yes I have seen that in Laravel. In Cake all it takes is setting the hasMany association with conditions, so perhaps just needs some documentation on how to setup polymorphic associations.

---

<div class="post-metadata">

**Author:** ![Erwane](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/erwane/32/3253_2.png) [@Erwane](https://discourse.cakephp.org/u/Erwane)\
**Post date:** [December 11, 2024, 5:01pm UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/5 "2024-12-11T17:01:15Z")

</div>

ok, i can start a draft in documentation.

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [December 11, 2024, 11:21pm UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/6 "2024-12-11T23:21:37Z")

</div>

If I wanted the `comments` to be linked to both `articles` & `products`, I would have done it like this

```auto
create table comments
(
	id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
	article_id	int unsigned default null,
	product_id	int unsigned default null,
	content text not null,

	FOREIGN KEY article_key ( article_id ) REFERENCES articles(id),
	FOREIGN KEY product_key ( product_id ) REFERENCES products(id),
	// modified & created ? 

);
// Ensure validation in src/Modal/Table/CommentsTable.php is correct

```

cakephp will automatically create the `src/Modal/Table/CommentsTable.php` with bake command with `belongsTo`

---

<div class="post-metadata">

**Author:** ![Erwane](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/erwane/32/3253_2.png) [@Erwane](https://discourse.cakephp.org/u/Erwane)\
**Post date:** [December 11, 2024, 11:50pm UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/7 "2024-12-11T23:50:06Z")

</div>

This is a way but if you have a “likes” table and propose to `like`, articles, comments, products, pictures, videos, artists, … tons of columns in your table.

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [December 12, 2024, 11:24am UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/8 "2024-12-12T11:24:42Z")

</div>

> [@yousuo](#):
>
> ```auto
> create table comments
> (
> id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
> related_id	int unsigned default null,
> related_table_type int unsigned not null,
> content text not null,
> 
> FOREIGN KEY article_key ( article_id ) REFERENCES articles(id),
> FOREIGN KEY product_key ( product_id ) REFERENCES products(id),
> // modified & created ? 
> 
> );
> 
> ```

I am just throwing some ideas. Not sure whether this is the best practice, but if there are lots of tables (linking to) using `comments`-table, I would link them with a field, such as `related_table_type` and in tables,

```auto
// example in src/Modal/Tables/CommentsTable.php
// Create constant. Or you can use enum on php8
const ARTICLES = 1000;

// example in src/Modal/Tables/ArticlesTable.php
$this->belongsTo('Comments', [
	'foreignKey' => 'comment_id', // Modify as needed
	'bindingKey' => 'related_id', // Modify as needed
	 'conditions' => [
		'Comments.related_table_type' => [CommentsTable::ARTICLES ,],               
	],
]);

```

---

<div class="post-metadata">

**Author:** ![dereuromark](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dereuromark/32/37_2.png) [@dereuromark](https://discourse.cakephp.org/u/dereuromark)\
**Post date:** [December 13, 2024, 3:02pm UTC](https://discourse.cakephp.org/t/dynamics-relations-for-comments-and-others-generic-tables/12333/9 "2024-12-13T15:02:59Z")

</div>

See also

> **[GitHub - dereuromark/cakephp-comments: Comments Plugin for CakePHP](https://github.com/dereuromark/cakephp-comments)**
>
> Comments Plugin for CakePHP

And other existing plugins for this very same topic.

The awesome list is always a good idea to take a look at regarding specific topics.
