php - codeigniter join changing id from one table's id to another

381

I have this code for an active record query in codeigniter:

$this->db->join('user', 'user.id = purchase_req.owner_id', 'inner');
$this->db->where('user.employer_id', $User->employer_id);
$Purchase_req = $this->Purchase_req->find();

In a view without the join statement,$Purchase_req->id would return the actual purchase request id. In the view with the join,$Purchase_req->id returns the user id. How can I join the tables together, and still get the purchase request id, instead of it changing to the user.id?

352

Answer

Solution:

The id you want to achieve is the ambiguous for mysql because the both tables have the id columns therefore when you tries to access the$Purchase_req->id it will return the last id column which is from thepurchase_req table you need to assingn the unique aliases for the same columns in the joined table like

$this->db->select('`user`.*,`purchase_req`.*,`user`.id AS user_id')
$this->db->join('user', 'user.id = purchase_req.owner_id', 'inner');
$this->db->where('user.employer_id', $User->employer_id);
$Purchase_req = $this->Purchase_req->find(); 

Now when you echo$Purchase_req->user_id it will return the user.id

People are also looking for solutions to the problem: php - Windows is not resing on Social Engine

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.