gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:20:09 24 4
gpt4 key购买 nike

我有下表

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回答,但他甚至说“这太荒谬了”

我的 Controller 代码

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

最佳答案

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

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

}

关于mysql - 使用 Grocery CRUD 连接具有不同字段名称的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21359915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com