简体   繁体   English

3张桌子之间的杂货杂货集关系

[英]Grocery Crud set relation between 3 tables

I have 3 sql tables and they are with this form. 我有3个sql表,它们与此表单一起使用。

TEST 测试

  • test_id 为test_id
  • test_name TEST_NAME

EXAMPLE

  • example_id example_id
  • example_test_id example_test_id

SOMETHING SOMETHING

  • something_id something_id
  • something_example_id something_example_id

So i want to take the name of the TEST table among with the id and send it to the table EXAMPLE, and then the example_id should be sent to SOMETHING table. 所以我想将TEST表的名称与id一起发送到表EXAMPLE,然后将example_id发送到SOMETHING表。 Finishing the relation i want the name of the TEST table to be shown by the example_id in the SOMETHING table. 完成关系后,我希望在SOMETHING表中的example_id显示TEST表的名称。

We have tried this 我们已经尝试过了

 $crud->set_relation_n_n('something','example','test','example_id','example_test_id','test_name');

And it takes the id of the TEST and it saves it to example_test_id but we want it to be saved at something_example_id and show the test_name. 它使用TEST的ID,并将其保存到example_test_id,但我们希望将其保存在something_example_id中并显示test_name。 All the tables have foreign keys and primary keys. 所有表都有外键和主键。 Hope you understand. 希望你能理解。

It would be easier for you if you were using custom model functions instead of set_relation_n_n function. 如果您使用自定义模型函数而不是set_relation_n_n函数,则对您来说会更容易。

First create a model function to get all the id and test_id from example table 首先创建一个模型函数以从示例表中获取所有id和test_id

public function get_example_list()
    {
        $this->db->select('example_id, example_test_id');
        $this->db->order_by('example_id asc');
        return $this->db->get('example')->result();
    }

Next, create a second model function to get tha test_name using the test_id from the first model 接下来,创建第二个模型函数以使用第一个模型中的test_id获得tha test_name

public function get_test_names($id)
    {
        $this->db->select('test_name');
        $this->db->from('test');
        $this->db->where('test_id', $id);
        $name=$this->db->get();
        return $name->row()->test_name; 
    }

in your controller use the above functions to create an array with the data you need 在您的控制器中,使用上述功能创建一个包含所需数据的数组

$list = $this->your_model->get_example_list();

        foreach ($list as $column => $data) {
            $myarray[$data->example_id] = $this->your_model->get_test_names($data->example_test_id);
        }

finally use "field_type" function to pass the array into something_example_id. 最后使用“ field_type”函数将数组传递给something_example_id。

$crud->field_type('something_example_id','dropdown',$myarray);

Now. 现在。 in something_example_id column you have a dropdown, displaying the test_name value and saving the example_id value on your database. 在something_example_id列中,您有一个下拉列表,显示test_name值并将example_id值保存在数据库中。

If you don't need separate drop-downs, you can create DB view on example table: 如果不需要单独的下拉菜单,则可以在示例表上创建数据库视图:

create view_example as
  select example.*, test.test_name as test_name
  from example left outer join test on 
    example.example_test_id = test.test_id

and then in your controller for table something_example_id: 然后在您的控制器中获取表格something_example_id:

$crud->set_primary_key('example_id','view_example');
$crud->set_relation('something_example_id', 'view_example', '{test_name}.{example_id}');

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

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