Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 3个表之间的Crud集合关系_Php_Mysql_Codeigniter_Foreign Keys_Grocery Crud - Fatal编程技术网

Php 3个表之间的Crud集合关系

Php 3个表之间的Crud集合关系,php,mysql,codeigniter,foreign-keys,grocery-crud,Php,Mysql,Codeigniter,Foreign Keys,Grocery Crud,我有3个sql表,它们与此表单一起使用 测试 测试id 测试名称 示例 示例\u id 示例\u测试\u id 某物 什么东西 某物\u示例\u id 因此,我想将测试表的名称与id放在一起 并将其发送到表EXAMPLE,然后将EXAMPLE_id发送到SOMETHING表。 完成关系后,我希望测试表的名称由SOMETHING表中的示例_id显示 我们已经试过了 $crud->set_relation_n_n('something','example','test','exa

我有3个sql表,它们与此表单一起使用

测试

  • 测试id
  • 测试名称
示例

  • 示例\u id
  • 示例\u测试\u id
某物

  • 什么东西
  • 某物\u示例\u id
因此,我想将测试表的名称与id放在一起 并将其发送到表EXAMPLE,然后将EXAMPLE_id发送到SOMETHING表。 完成关系后,我希望测试表的名称由SOMETHING表中的示例_id显示

我们已经试过了

 $crud->set_relation_n_n('something','example','test','example_id','example_test_id','test_name');
它获取测试的id,并将其保存到example\u TEST\u id,但我们希望它保存在example\u id,并显示测试名称。 所有表都有外键和主键。
希望您能理解。

如果您使用的是自定义模型函数,而不是设置关系函数,您会更容易理解

首先创建一个模型函数,从示例表中获取所有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();
    }
接下来,创建第二个模型函数,使用第一个模型中的test_id获取test_名称

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; 
    }
在控制器中,使用上述函数创建包含所需数据的阵列

$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);
        }
最后使用“field\u type”函数将数组传递到某物\u示例\u id中

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

现在。在“示例id”列中有一个下拉列表,显示测试名称值并将示例id值保存在数据库中。

如果不需要单独的下拉列表,可以在示例表上创建DB视图:

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
然后在您的控制器中,为表指定一些内容\u示例\u id:

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