Messages table of DB with two fields of user ID in Name Conventions


#1

I just begin to learn PHP and CackePHP 3.
I try to design DB.
I would like to realize messages table.
Users can post messages each other.
I have users table.
As I see it should be many-to-many relaction.
In messages table I need 2 fields with user_id. The first user_from_id, the second user_to_id.
But there are Name Conventions in Cake.

How should I realize comments table in view of Name Conventions? How should I name the first user_id field and the second one?

Am I right when I think that I need this fields?

CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
userto_id INT NOT NULL,
message TEXT NOT NULL,

FOREIGN KEY user_key (user_id) REFERENCES users(id),
FOREIGN KEY user_key (userto_id) REFERENCES users(id)

);

Am I right? And how it will be better realize in model and controller?

Thank you for your wish to help.


#2

Hi,
Your setup is correct. Just make sure to correct your table creation to this;

CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
userfrom_id INT NOT NULL,
userto_id INT NOT NULL,
message TEXT NOT NULL,

You can also learn from the tutorial, as the ‘articles’ are related to ‘users’ directly upon creation, including login and authorization.
If you use “bake” then the table and the foreign key relations will help you set up the right code.
Good luck.


#3

This is that I want to know.
I red tutorial but obviously I have understood this partly. I will try more.
Thank you, lafeber