简体   繁体   English

设置关系杂货crud错误

[英]set relation grocery crud error

I have 2 tables 我有2张桌子

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;

and

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;

As you can see table patient_medicine is the intermediate table of between tbla_medicines, and table patient. 如您所见,表patient_medicine是tbla_medicines和table patient之间的中间表。

Now I want to consult all data from tbl_patient_medicine with grocery crud like in this sqlfiddle 现在我想咨询来自tbl_patient_medicine的所有数据和杂货店crud, 就像在这个sqlfiddle中一样

supossing I pass the id in the uri (in the example will be id_patient=1) I have 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 I have tried different ways but got errors like this: 所以我尝试过不同的方法,但是遇到了这样的错误:

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

Line Number: 87 行号:87

I did your example: 我做了你的榜样:

Controller: 控制器:

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); 
}

It works! 有用!

Edited: 编辑:

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

Try by changing WHERE j7a675883. 尝试通过更改WHERE j7a675883。 id_patient

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

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