简体   繁体   English

动态表和模型在Laravel 5中

[英]Dynamic tables and models in Laravel 5

I have the next tables: 我有以下表格:

pages
    id
    title
    ...

dynamic_table_1_infos
    id
    page_id
    image_id
    dynamic_field_1
    dynamic_field_2
    ...

dynamic_table_2_infos
...

So there are "one to many" relationships. 因此,存在“一对多”关系。

Is there any way to use a general approach without creating a DynamicTable[No]Info model "on the fly" for each table? 有什么方法可以使用通用方法,而不必为每个表“动态”创建DynamicTable [No] Info模型?

And what if I will need some extra methods in these models? 如果我在这些模型中需要一些额外的方法怎么办?

The "Page" model will have many relationships with "dynamic_table_[no]_infos" tables. “页面”模型将与“ dynamic_table_ [no] _infos”表具有许多关系。 So I will probably need a general method for doing this. 因此,我可能需要一种通用的方法来执行此操作。 (Has many dynamic_table_1_infos / has many dynamic_table_2_infos ...) (具有许多dynamic_table_1_infos /具有许多dynamic_table_2_infos ...)

What I am trying to do is somehow inspired by Dupal`s content types. 我试图做的是受Dupal内容类型的启发。

If you want to save a single model data to multiple tables depending on the value of some model attribute, you'll need to override model's getTable() method to return the name of the table it should write to. 如果要根据某个模型属性的值将一个模型数据保存到多个表中,则需要重写模型的getTable()方法以返回应写入的表的名称。

I'm not sure how you want to decide to which table Eloquent should save to - for example, if you had an attribute called segment in your mode, you could just do: 我不确定如何确定Eloquent应该保存到哪个表-例如,如果您的模式中有一个名为segment的属性,则可以执行以下操作:

public function getTable() {
  return 'dynamic_table_' . $this->segment . '_infos';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM