使用Grocery CRUD连接具有不同字段名称的表

时间:2014-01-26 05:01:29

标签: mysql codeigniter grocery-crud

我有以下表格

CREATE TABLE IF NOT EXISTS `maintrequests` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `propID` int(11) DEFAULT NULL,
  `landlordID` int(11) NOT NULL,
  `subject` varchar(45) DEFAULT NULL,
  `message` varchar(200) DEFAULT NULL,
  `status` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(45) DEFAULT NULL,
  `firstname` varchar(45) DEFAULT NULL,
  `lastname` varchar(45) DEFAULT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

我对Grocery CRUD很新,并且在加入这两个表时遇到了问题。联接将是maintrequests.landlordID = users.id。 Grocery CRUD站点上的所有示例都将相关字段具有相同名称的表连接起来

我确实找到this回答,但他甚至说“这太荒谬了”

我的控制器代码

public function test(){
    $crud = new grocery_CRUD();
    $crud->set_subject('Maintenance Requests')->set_table('maintrequests')->columns('subject','created','status');
    $this->data['maintlist'] = $crud->render();


    $this->data['title'] = 'Maintenance Test';
    $this->load->view('global/_layout_main_test',$this->data);
}

我的模型功能,我正在尝试转换为Grocery

public function get_all_for_landlord_table($landlordid){
    return $this->db->select('maintrequests.subject,maintrequests.created,maintrequests.status,maintrequests.message,properties.address,units.unitnum')->order_by('maintrequests.created','asc')->from('maintrequests')->join('properties','maintrequests.propid = properties.id')->join('units','maintrequests.unitid = units.id')->where(array('maintrequests.landlordID'=>$landlordid))->get()->result();
}

2 个答案:

答案 0 :(得分:1)

试试这段代码。我没有测试。

public function get_all_for_landlord_table($landlordid){

    $crud = new grocery_CRUD();
    $crud->set_table('maintrequests');
    $crud->set_relation('maintrequests.propid','properties','properties.id');
    $crud->set_relation('maintrequests.unitid','units','units.id');
    $crud->where('maintrequests.landlordID',$landlordid);
    $output = $crud->render(); 

}

答案 1 :(得分:0)

我遇到了同样的问题。只需使用:

$crud->set_table('maintrequests');
$crud->set_relation('**landlordID**','**users**','**firstname**');

如果**users**表没有**landlordID**键,Grocery将获得PRIMARY KEY表的**users**