2012-08-17 83 views
1

我有兩張表,我想加入GroceryCRUD。我的控制器的Active Record,CodeIgniter - 「on子句不明確」

部分:

/* 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 

回答

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 
+0

更新的答案。看看ON條款 – 2012-08-17 16:54:04

+0

完美工作。謝謝! 當我讓我的時候,我會接受你的回答。 – 2012-08-17 16:56:44

相關問題