1

我有两张表我想加入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
4

1 回答 1

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
于 2012-08-17T16:50:17.300 回答