-1

My Client Table

Hey! I need the New Location button on the main client database table to create a new database table. How do I do that?

These tables would be the client list for each location. Each client would have a location id that would place them in the correct table. This way we would have a Client List Table with all clients from all locations (image 1) and a separate table for each location.

Cakephp version - 4.0.7

ndm
  • 59,784
  • 9
  • 71
  • 110
Peralta
  • 1
  • 1
  • 1
    What does "create a new table" mean? What have you got so far? – Greg Schmidt May 24 '20 at 18:20
  • Sorry. I have a table that contains all clients and is suppose to be the primary page. I then want to have a series of linked tables that are meant to contain clients from a location. This means when I add a client on the main table one of the parameters of said client has a location code that adds that client to the corresponding table. What I need is to add a button to the main table that allows the user to create a new location table. – Peralta May 24 '20 at 19:18
  • When you say "table" in all of this, you're referring to database tables, right? Not the HTML `` tag? You'd be well served by editing your question to include some details about the schemas of the tables in question, and clarify exactly what records you want to be created at what times in the process you're working on.
    – Greg Schmidt May 24 '20 at 22:48
  • I changed the info. Sorry I'm new all around. – Peralta May 25 '20 at 14:49
  • I could really use your help. Please! – Peralta May 26 '20 at 00:56
  • Why would you want to do that? There's only very few, very specific scenarios where multiple tables instead of multiple rows makes any sense whatsoever! Also, from what you're describing, it sounds as if your separate tables would only hold a single row, which would make it even more questionable? You'll probably hardly get any good answers if you can't provide an explanation for your intended solution, as people might very well perceive it as an [**XY problem**](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – ndm May 26 '20 at 12:42

1 Answers1

0

If I understand correctly you want to linked one client to one shop location (or maybe many location).

First way is one to Many. (client belongs to location and location hasMany clients) You need to create table with you clients and put new column like location_id. Then simply set one of many location to client (in edit or use postButton with location_id and client_id).

Secondly way is many to many. That means many clients belongs to many locations and many locations belongs to many clients. Like tags in articles. You need to create three tables. One with clients, One with locations and One to connect it together.

In this way Clients Table and Location Table dont't have any additional columns to direct to themselfs (like location_id or client_id). You need to make third table with name clients_locations and add colmuns with ID of Client Table and Location Table. Simple example:

   +--------+-------------+-----------+
   | id.    | location_id | client_id |
   +--------+-------------+-----------+
   |   1    |       3     |    10     |
   +--------+-------------+-----------+
   |   2    |       4     |     3     |
   +--------+-------------+-----------+
   |   3    |       7     |     9     |
   +--------+-------------+-----------+
   |   4    |       4     |    40     |
   +--------+-------------+-----------+

In Cakephp its name as BelongsToMany.

Oliwer Lisek
  • 126
  • 5
  • Sorry, I'm new and it really wasn't posed very eloquently. I edited the question to better explain. – Peralta May 25 '20 at 14:50
  • I could really use your help, please! – Peralta May 26 '20 at 00:57
  • Why you want to create a new database table for each click for button? It doesn't make sense. – Oliwer Lisek May 26 '20 at 06:46
  • I understand your reservations but I need to do it. It's not up to me. Please – Peralta May 26 '20 at 11:40
  • I don't understand. It is a simple thing. I explained you how to do it. It doesn't depend on CakePHP. This is generally SQL solution. All you need is inside Cake documentation. Check: https://book.cakephp.org/4/en/orm/associations.html#belongstomany-associations – Oliwer Lisek May 27 '20 at 08:45