php - 设置关系 Grocery crud 错误

标签 php mysql codeigniter join grocery-crud

我有两张 table

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 和表 patient 之间的中间表。

现在我想使用 grocery crud 查看来自 tbl_patient_medicine 的所有数据 like in this sqlfiddle

假设我在 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());
     }      
}

所以我尝试了不同的方法但得到了这样的错误:

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

最佳答案

我做了你的例子:

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

有效!

已编辑:

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

关于php - 设置关系 Grocery crud 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15491457/

相关文章:

php - $_SESSION ['uiD' ] 注册后无法重定向到主页,uiD 未设置

php - 将Mysql结果输出到数组中

php - 重定向后 Codeigniter session 数据丢失

php - 如何在 CodeIgniter 的 url 中隐藏 Controller 名称?

php - 使用 CodeIgniter 的图片库

php - 输入题 mysql php html

php - 更改 API 数据输出的布局

python - 如何在两个 Django 应用程序之间移动模型(Django 1.7)

php - Php 中只显示我的整数值的第一位

javascript - AngularJS PHP CORS 不适用于 FireFox