简体   繁体   English

加入表Codeigniter时字段列表中的列不明确

[英]Ambigous column in field list when join table codeigniter

I'm trying to join two tables and show the data with ajax, codeigniter, and active record, but suddenly shows an error: 我试图联接两个表,并用ajax,codeigniter和活动记录显示数据,但突然显示错误:

Column 'userfile' in field list is ambiguous 字段列表中的“用户文件”列不明确

Table structure: 表结构:

Album: id_album, judul_album, userfile, userfile_type 相册:id_album,judul_album,userfile,userfile_type

Photo: id_photo, id_album, judul_photo, userfile, userfile_type 照片:id_photo,id_album,judul_photo,userfile,userfile_type

Controller : 控制器

public function ajax_list()
{
  $list = $this->Photo_model->get_datatables();
  $data = array();
  $no = $_POST['start'];

  foreach ($list as $data_foto) 
  {
    $no++;
    $row = array();
    $row[] = '<p style="text-align: center">'.$no.'</p>';
    $row[] = '<p style="text-align: left">'.$data_foto->judul_foto.'</p>';
    $row[] = '<p style="text-align: left">'.$data_foto->id_album.'</p>';
    $row[] = '<p style="text-align: center"><img src="'.base_url('assets/images/photo/').$data_foto->userfile.$data_foto->userfile_type.'" width="100px"></p>';

    $row[] = "
    <p style='text-align: center'>
    <a class='btn btn-sm btn-warning' href='".base_url('admin/photo/update/').$data_foto->id_photo."' title='Edit'><i class='fa fa-pencil'></i> EDIT</a>
  </p>
  <p style='text-align: center'>
    <a class='btn btn-sm btn-danger' href='".base_url('admin/photo/delete/').$data_foto->id_photo."', onClick=\"return confirm('Are you sure?');\"><i class='glyphicon glyphicon-trash'></i> DELETE</a>
  </p>";

  $data[] = $row;
}

$output = array(
          "draw" => $_POST['draw'],
          "recordsTotal" => $this->Photo_model->count_all(),
          "recordsFiltered" => $this->Photo_model->count_filtered(),
          "data" => $data
          );
//output to json format
echo json_encode($output);
}

Model : 型号

public function get_datatables()
{
  $this->_get_datatables_query();
  if($_POST['length'] != -1)
  $this->db->select('id_foto,judul_foto,foto_seo,ket,userfile as ufile,userfile_type as ufile_type');
  $this->db->join('album ', 'album.id_album = foto.id_photo');

  $query = $this->db->get();

  return $query->result();
}

Any help will be so appreciate. 任何帮助将不胜感激。

You need to specify which table you want to get the columns from where the columns have the same name. 您需要指定要从中具有相同名称的列中获取列的表。 EG. 例如。 album.id_album and photo.id_album . album.id_albumphoto.id_album So you select should look like this 所以你选择应该看起来像这样

$this->db->select('id_foto,judul_foto,foto_seo,ket,[album|photo].userfile as ufile,[album|photo].userfile_type as ufile_type');

Replacing [album|photo] with whichever table you need the column to come from. 用需要该列的表格替换[album|photo] If you need it from both then the below would work 如果您都需要它,那么下面的方法会起作用

$this->db->select('id_foto,judul_foto,foto_seo,ket,album.userfile as album_ufile,album.userfile_type as album_ufile_type,photo.userfile as photo_ufile,photo.userfile_type as photo_ufile_type');

You should use table_name . 您应该使用table_name column name to solve ambiguous problem. column name解决模棱两可的问题。

Try this way: 尝试这种方式:

public function get_datatables()
{
    $this->_get_datatables_query();
    if ($_POST['length'] != -1) {
        $this->db->select('id_foto, judul_foto, foto_seo,ket, album.userfile as ufile, album.userfile_type as ufile_type');
    }

    $this->db->join('album ', 'album.id_album = foto.id_photo');
    $query = $this->db->get();

    return $query->result();
 }

thanks everyone, finally it solved with: 谢谢大家,终于解决了:

$this->db->select('id_foto, judul_foto, judul_album, foto.uploader as uploaders,foto.userfile as ufile,foto.userfile_type as ufile_type');
$this->db->join('album ', 'album.id_album = foto.id_album');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM