douduan9129 2015-12-02 15:45
浏览 23

Laravel 5.1在Model :: find上的Eloquent更改表

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.

  • 写回答

1条回答 默认 最新

  • doudianhuo1129 2015-12-02 20:32
    关注

    There is the issue:

    To change the table name of the Model when you want to update data of a row, you simply need to add a function inside the model :

    public function getTable() 
    {
        return 'tableName';
    }
    

    So, for my app, I needed to add this after the namespace:

    use Auth;
    

    And then add this function :

    public function getTable() 
    {
        return 'd'.Auth::user()->dealer_id.'clients';
    }
    

    So when you update a row, you can simply do this:

    $client =  Client::find(Request::get('clientID'));
    $client -> last_name = Request::get('up_last_name');
    $client -> first_name = Request::get('up_first_name');
    $client -> phone = Request::get('up_phone');
    $client -> cellphone = Request::get('up_cellphone');
    $client -> email = Request::get('up_email');
    $client -> civic = Request::get('up_civic');
    $client -> road = Request::get('up_road');
    $client -> city = Request::get('up_city');
    $client -> province = Request::get('up_province');
    $client -> postal_code = Request::get('up_postal_code');
    $client -> birth_date = Request::get('up_birth_date');
    $client -> driving_liscence = Request::get('up_driving_liscence');
    $client -> touch();
    $client -> save();
    

    This way, MySQL will search for the row that need to be update directly in the right table.

    So, if what I talked about the way to speed up the work when you work with huge amount of data, this way will be faster then creating two table and a pivot table.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持