2013-03-19 102 views
3

我有2个表组关系杂货店CRUD错误

CREATE TABLE `tbl_patient` (
    id_patient  INTEGER  NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    name   VARCHAR(25) NOT NULL DEFAULT "not available", 
    att1   VARCHAR(5) NOT NULL DEFAULT "att1", 
    att2   VARCHAR(25) NOT NULL DEFAULT "att2", 
    att3   VARCHAR(25) NOT NULL DEFAULT "att3", 
    CONSTRAINT `uc_Info_patient` UNIQUE (`id_patient`)   
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 

CREATE TABLE `tbl_patient_medicine` ( 
    id_patient_medicine INTEGER  NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    id_patient   INTEGER  NOT NULL, 
    name_medicine  VARCHAR(50) NOT NULL DEFAULT "", 
    dosis    VARCHAR(50) NOT NULL DEFAULT "",   
    start_date   timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    treatment   VARCHAR(50) NOT NULL DEFAULT "", 
    times_per_day  VARCHAR(50) NOT NULL DEFAULT "",  
    CONSTRAINT fk_ID_Patient_Medicine FOREIGN KEY (id_patient) REFERENCES `tbl_patient`(id_patient) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 

正如你可以看到表patient_medicine是tbla_medicines之间的中间表,表病人。

现在我想用杂货污物协商从tbl_patient_medicine所有数据like in this sqlfiddle

supossing我通过在URI中的ID(在本例将id_patient = 1) 我有

public function details_medication($my_id = 0) 
{ 
    try{ 
    $crud = new grocery_CRUD(); 
    $crud->where('id_patient',$my_id); 
    $crud->set_table('tbl_patient_medicine'); 


     //HOW TO DO IT? 
     $crud->set_relation('id_patient', 'tbl_patient', 'id_patient'); 


    $output = $crud->render(); 
     $this->_output($output); 

    }catch(Exception $e){ 
    show_error($e->getMessage().' --- '.$e->getTraceAsString()); 
    }  
} 

SO我尝试了不同的方式,但得到这样的错误:

A Database Error Occurred 

Error Number: 1052 

Column 'id_patient' in where clause is ambiguous 

SELECT `tbl_patient_medicine`.*, j7a675883.id_patient AS s7a675883 
FROM (`tbl_patient_medicine`) 
LEFT JOIN `tbl_patient` as j7a675883 
ON `j7a675883`.`id_patient` = `tbl_patient_medicine`.`id_patient` WHERE `id_patient` = '1' LIMIT 25 

行号:87

+0

我不知道,我想这就是它使用不同的列名引起problem.Try的主键,试试这个,$ crud-> set_relation( 'id_patient',' tbl_patient','name');试试n temme O/p – Christopher 2013-03-19 09:27:17

回答

3

我做你的榜样:

控制器:

function medicine() 

{ 
    $crud = new grocery_CRUD(); 

    $crud->set_table('tbl_patient_medicine'); 
    $crud->required_fields('id_patient','name_medicine','dosis','start_date','treatment','time_per_day'); 
    $crud->columns('id_patient','name_medicine','dosis','start_date','treatment','time_per_day'); 
    $crud->fields('id_patient','name_medicine','dosis','start_date','treatment','time_per_day'); 

    $crud->set_relation('id_patient','tbl_patient','name'); 

    $output = $crud->render(); 

    $this->_example_output($output); 
} 

它的工作原理!

编辑:

$crud->set_relation('id_patient','tbl_patient','{name}, {att1}, {att2}, {att3}'); 
+0

自动生成的,你可以让我发布完整的代码,因为我认为我做的事情非常错误? – cMinor 2013-03-20 16:03:20

+0

在这里你有完整的控制器。测试它,它会起作用。 – 2013-03-21 12:26:42

+0

好吧,我已经尝试过它,它的工作原理,但是有没有办法让'tbl_patient'的所有字段,如ID,NAME,att1,att2,att和'tbl_patient_medicine'的所有字段?在您的解决方案中,我只能在字段ID中获取患者姓名,如何从“tbl_patient”中获取所有字段 – cMinor 2013-03-22 06:48:09

-1

尝试改变WHERE j7a675883。 id_patient

+3

好吧,我不能因为它是由grocerycrud – cMinor 2013-03-19 04:56:12