【问题标题】:set relation grocery crud error设置关系杂货杂货错误
【发布时间】:2013-03-07 15:40:39
【问题描述】:

我有两张桌子

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;

如您所见,table patient_medicine 是 tbla_medicines 和 table patient 之间的中间表。

现在我想用杂货杂货like in this sqlfiddle查询来自 tbl_patient_medicine 的所有数据

假设我在 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

【问题讨论】:

  • 我不确定,我猜它是导致问题的主键。尝试使用不同的列名,试试这个,$crud->set_relation('id_patient', 'tbl_patient', 'name' );试试看 o/p

标签: php mysql codeigniter join grocery-crud


【解决方案1】:

尝试更改 WHERE j7a675883.id_patient

【讨论】:

  • 好吧,我不能,因为那是由grocercrud自动生成的
【解决方案2】:

我做了你的例子:

控制器:

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

【讨论】:

  • 能否请您发布完整的代码,因为我认为我做错了什么?
  • 这里有完整的控制器。测试一下,它会起作用的。
  • 好的,我已经尝试过了,它可以工作,但是有没有办法获取tbl_patient 的所有字段,如ID、NAME、att1、att2、att 和tbl_patient_medicine 的所有字段?在您的解决方案中,我只能在字段 ID 中获取患者姓名,如何从 tbl_patient获取所有字段@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多