我想要加入GroceryCRUD两个表。
我的控制器的一部分:
/* Generate CRUD for buildings table */
function buildings()
{
try{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('buildings');
$crud->set_subject('Campus Building');
$crud->columns('building_code', 'building_name', 'description', 'latitude', 'longitude');
$crud->add_action('Images', '', '','ui-icon-image',array($this,'image_gallery_action')); // link to building image gallery
$crud->fields('building_code', 'building_name', 'description', 'latitude', 'longitude', 'created_on', 'updated_on'); // only display building_code, building_name, description and lat/long in the add/edit view
$crud->change_field_type('created_on', 'invisible');
$crud->change_field_type('updated_on', 'invisible');
$crud->set_model('gallery_join_model'); // model created to join building and building_galleries
$crud->callback_after_insert(array($this, 'gallery_urls_after_insert')); // auto generate iphone and ipad gallery urls on insert
$crud->callback_before_delete(array($this, 'gallery_urls_before_delete')); // auto delete iphone and ipad gallery urls on delete
$output = $crud->render();
$this->_example_output($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
连接表的模型:
<?php
class gallery_join_model extends grocery_CRUD_Model {
// --------------------------------------------------------------------
/**
* Initialization function (__construct)
*
* Initialize, get parent properties and retrieve any messages
*
* @access public
* @return void
*/
public function __construct(){
parent::__construct();
}
function get_list() {
if($this->table_name === null) {
return false;
}
$select = "{$this->table_name}.*";
$_select = array(
'building_galleries.gallery_id',
'building_galleries.building_id g1',
'building_galleries.gallery_ipad_url',
'building_galleries.gallery_url',
);
$select .= ','. implode(',', $_select);
if( ! empty($this->relation))
foreach ($this->relation as $relation) {
list($field_name , $related_table , $related_field_title) = $relation;
$unique_join_name = $this->_unique_join_name($field_name);
$unique_field_name = $this->_unique_field_name($field_name);
if (strstr($related_field_title,'{')) {
$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
}
else {
$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
}
if ($this->field_exists($related_field_title)) {
$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
}
}
$this->db->select($select, false);
$this->db->join('building_galleries','building_id = g1');
$results = $this->db->get($this->table_name)->result();
return $results;
}
}
错误:
Error Number: 1052
Column 'building_id' in on clause is ambiguous
SELECT buildings.*, building_galleries.gallery_id, building_galleries.building_id g1, building_galleries.gallery_ipad_url, building_galleries.gallery_url FROM `buildings` JOIN `building_galleries` ON `building_id` = `g1`
Filename: C:\xampp\htdocs\system\database\DB_driver.php
Line Number: 384
答案 0 :(得分:2)
您需要在ON
列前添加别名或表名称。
SELECT buildings.*, building_galleries.gallery_id, building_galleries.building_id g1, building_galleries.gallery_ipad_url, building_galleries.gallery_url FROM `buildings` JOIN `building_galleries` ON `building_id` = `g1`
应该是:
SELECT buildings.*, building_galleries.gallery_id, building_galleries.building_id g1, building_galleries.gallery_ipad_url, building_galleries.gallery_url FROM `buildings` JOIN `building_galleries` ON `buildings.building_id` = building_galleries.building_id