In a previous question, I was looking for save query using a Model and change the table when I add a new query. I found my answer with some tips by you. Now, when I update a row, I'm unable to retrieve this row because the Model::find use the tablename set in the model. I tried to use this:
$client = Client::fin(something);
$client -> setTable(newTableName);
$client -> things to update...
$client -> save();
This doesn't work. I tried some variants like this:
$client = Client->setTable(newTableName)::find(something);
But it doesn't work too... And I tried some other things.
Now, I'm asking for your help to find the issue.
What I know, when you use Model::find, it call a function like this:
public function getQualifiedKeyName()
{
return $this->getTable().'.'.$this->getKeyName();
}
public function getKeyName()
{
return $this->primaryKey;
}
But I am not really sure how I can modify this to input some variable in the get table name to find the row in the right table.
The table associate with the model Client is clients but it should be:
'd'.Auth::user()->dealer_id.'clients'
So if the user is associate with Dealer ABC and the id of this dealer is 1, the tablename will be d1clients.
Can you show me the right direction?
Thank you all!
EDIT : I added this section because I needed some space to explain why I'm trying to go this way.
This is how I understand the work behind the scene.
There is a database which contain table.
Each table contain rows.
When you want to associate row from table A to row from table B, you create a pivot table to make the association. When you request to find data in the table, the serve look into the pivot table to find which rows from table B are associate to the row from table A. Example, just to show you if I understand well:
Table users:
1 | username
2 | username
3 | username
Table clients:
1 | clientName
2 | clientName
3 | clientName
4 | clientName
Pivot table:
users | clients
1 | 1 - 3
2 | 2
3 | 4
When User #1 request to update Client # 1, the server look into pivot table wich clients are associate to User #1, then, if the Client #1 is associate to User #1, it goes into clients table and get the data from Client #1.
This seems to be easy to set up, but if I'm right, if I have 1 000 users and each users have 1 000 clients, it result of 1 000 rows in users table, 1 000 rows in pivot table and 1 000 000 rows in clients table. For me, it look like the serve need to process a lot of data. This is not a real problem if you got a powerful server. But it's not my case.
What I try to do, with how I understand all of this, is to make smaller table to make the process quicker. If I put this 1 000 000 rows in different tables, I will get 1 table for users, 1 000 tables of 1 000 rows. When the users will request for update to a client, the system will look at the user, then find the table associated with this user and then get the row requested in a table that only contain clients who are associated to this user.
If I am right, the process with a huge amount of data will be shorter. Only if I am right.