简体   繁体   中英

codeigniter JOIN returing wrong id

I'm new to codeigniter and have got stuck on an issue with a JOIN.

i have 2 tables

super_admin_staff and super_admin_roles

super_admin_staff has a unique id column named 'id' it also has a column named 'role'

the 'role' column corresponds to the 'id' column of super_admin_roles.

my model :

 function superAdminStaff()
{
    $this->db->select('*');
    $this->db->from('super_admin_staff');
    $this->db->join('super_admin_roles', 'super_admin_staff.role = super_admin_roles.id');


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

    $result = $query->result();
    return $result;
}

my class :

public function index()
{

    if ($this->isSuperAdmin() != true) {
        $this->loadThis();
    } else {

        $this->load->model('super_admin_staff_model'); 

        $data['userRecords'] = $this->super_admin_staff_model->superAdminStaff(); 

        // set up page title and description
        $this->global['pageTitle'] = 'Staff List';
        $this->global['pageDesc'] = 'Add, Edit or Resign Staff';

        $this->loadViews("super_admin/staff", $this->global, $data, null); 
    }
}

my view :

<tbody>
      <?php
      if (!empty($userRecords)) {
        foreach ($userRecords as $record) {
          ?>
          <tr>

            <td><img src="<?php echo base_url() . 'uploads/super_admin/' . $record->profile_picture ?>" class="wd-100 rounded-circle bd bd-2"></td>
            <td class="align-middle"><?php echo $record->firstname ?></td>
            <td class="align-middle"><?php echo $record->lastname ?></td>
            <td class="align-middle"><?php echo $record->email ?></td>
            <td class="align-middle"><?php echo $record->phone ?></td>
            <td class="align-middle"><?php echo $record->role ?></td>
            <td class="text-center align-middle">

              <a class="btn btn-sm btn-danger deleteUser" href="<?php echo base_url() . 'super_admin_staff/deletestaff/' . $record->id; ?>" data-userid="<?php echo $record->id; ?>"><i class="fas fa-trash"></i></a>
            </td>
          </tr>
      <?php
        }
      }
      ?>

    </tbody>

everything works as expected and <?php echo $record->role ?> returns the name of the role

but the <?php echo $record->id ?> is now replaced with the id from the super_admin_roles table and not super_admin_staff as expected.

all im trying to do is get the role name from super_admin_roles using the id that is stored in the super_admin_staff.roles column and still use the id from the super_admin_staff table so i can delete a staff member based on id.

Column id is ambiguous, since it exists in both tables that are involved in the query.

One solution would be to enumerate the columns that you want the query to return. Table aliases are also handy to shorten the query.

So something like:

$this->db->select('s.firstname, s.last_name, s.email, s.email, s.phone, s.role, s.id')
$this->db->from('super_admin_staff as s');
$this->db->join('super_admin_roles as r', 's.role = r.id');

I am unsure that you actually need columns fro the super_admin_roles table. If you do, just add them to the select .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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