I have 2 tables that i want to join & show the name of user's role. here's the situation
My 2 tables are users_mlh & user_roles_mlh
on the role column of users_mlh table i'm storing the ID of user role, user_roles_mlh contains the name & id of user role. what i want to do is show the name of the user role in my view.
my tables as follows.
i have tried this in my model
$this->db->select('*');
$this->db->from('user_roles_mlh');
$this->db->join('users_mlh', 'users_mlh.role = user_roles_mlh.id');
$this->db->where('users_mlh.role = user_roles_mlh.id');
$query = $this->db->get();
return $query->result_array();
but from above i get something like this
at the moment it lists all user level not the role of each individual user
Case 1 : If you Directly want to Access all data without using where condition
$this->db->select( "*" );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
Case 2: With Where Condition and specific column data
$this->db->select( 'user_roles_mlh.role_type,users_mlh.name' );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
$this->db->where('users_mlh.id =',1);
And finally get the results by
$query = $this->db->get();
return $query->result();
No need to write separate query for getting role name. Join Roles table when you fetching users data..
$this->db->select('users_mlh.*,user_roles_mlh.role_type');
$this->db->from('users_mlh');
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
$query = $this->db->get();
return $query->result_array();
Try this.
If I use your tables, you should have the following request:
SELECT users_mlh.*, JOIN user_roles_mlh.role_type
FROM users_mlh -- list all users
LEFT JOIN user_roles_mlh -- and joind by roles
ON users_mlh.role = user_roles_mlh.id -- condition for joining
With this request, you will have all your users, with the role text associated.
So, if this request works, use the same logic on your ORM
Related
My application works like this: α user creates a task and assigns it
to another user. I have two tables
(user table)
user_name
AND
(task table)
task_subject
creator
assigned_to
my question how to make join statement by codeigniter to display
the task, its creator name and assigned to whom.
Please check following query for join in codeigniter
$user = 'user';
$task = ' task,';
$this->db->select($user.'.*,'.$task.'.*');
$this->db->from($task);
$this->db->join($user,
$task.'.user_id = '.$user.'.user_id'
);
$this->db->join($user,$user.'.user_id = $task.'created_by','left');
$query = $this->db->get();
if ($query->num_rows() > 0){
$result = $query->result();
return $result;
}
return FALSE;
Instead of using * you can use your own custom fields that you want to show and in join use the common id in both tables like I use user_id
In my application, there's two tables at the moment: users and employees. The latter is simply a profile table for the boilerplate users table that ships with Laravel.
I have two foreign keys inside of employees, one is user_id that points to id on users table. So basically, the profile data of the registered user. The second foreign key in employees is manager_id. That is the id of the person who is the manager of an employee. Some users are managers and have a blank manager_id field.
I want to retrieve the row from my Employee model where the user_id matches the manager_idand then send them all to my view, where they will see the first_name and last_name of the manager. At the moment I see the manager ID instead of their name.
Sample LeftJoin =>
DB::connection('testing')->table('table1 as t1')
->select('t1.column, t2.column')
->leftJoin('table2 as t2','t2.client_id','=','t1.id')
->get();
Try some thing like this:
$users = DB::table('users t1')
->join('users t2', 't1.id', '=', 't2.user_id')
->select('your column list')
->get();
// here we are creating 2 aliases for users table which make it self join
In your Employees model add the following method:
public function manager()
{
return $this->hasOne('App\User', 'id', 'manager_id');
}
You can then get the name of the Manager like this:
$employee = \App\Employee::find(1);
$manager = $employee->manager()->first();
$manager_name = $manager->name;
I'm using Codeigniter and I need to join 2/3 tables. I can't quite work it out.
I have a users table with general user info.
user_id
first_name
last_name
I have a course stats page with info like due date etc.
id
course_id
user_id
due
completed_on
A user will pick a course, and then be presented with a list of users of their organization. Ready to be assigned. The user may already be assigned to the course and have stats in the stats table while other users may not have stats.
So I need to display a list of users with any stats to the chosen course, if the stats do of course exists.
SO, I'm sending to the model an org_id (to get a list of users of that org) and the chosen course_id (to retrieve any stats from he stats table if they exist)
Here's my method in the model
// get users for an org along with stats
public function get_course_user_data($org_id,$course_id) {
$user_array = array('users.org_id =' => $org_id);
$this->db->select('users.id,users.first_name,users.last_name,training_stats.due,training_stats.completed_on');
$this->db->order_by('users.first_name', 'asc');
$this->db->where($user_array);
$this->db->join('training_stats','training_stats.user_id = users.id', 'left');
$this->db->join('training_courses','training_courses.course_id = training_stats.course_id', 'left');
$user_object = $this->db->get('users');
return $user_object->result();
}
At the moment, if there are stats in the stats table then it displays data, if there are no stats in the stats table, I get nothing.
But I need a full list of users and only the stats if they exist.
Can I even do this with a join? I would have though so?
Try this query:
// get users for an org along with stats
public function get_course_user_data($org_id,$course_id) {
$this->db->select('users.id,users.first_name,users.last_name,training_stats.due,training_stats.completed_on');
$this->db->order_by('users.first_name', 'asc');
$this->db->join('training_stats','training_stats.user_id = users.id', 'left');
$this->db->join('training_courses','training_courses.course_id = training_stats.course_id', 'left');
$this->db->where("( training_courses.course_id = $course_id AND training_stats.course_id = $course_id)" );
$this->db->or_where( "( users.org_id = $org_id )" );
$user_object = $this->db->get('users');
return $user_object->result();
}
I think you forgot something...
$this->db->from('table_name');
after this....
$this->db->select('users.id,users.first_name,users.last_name,training_stats.due,training_stats.completed_on');
I hope this help
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?
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 the purchase_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
hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work
$this->db->join();
what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.
i have tried to code a function using this example:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
EDIT:
instead of using join method in codeigniter is it possible to use a simple function to retrieve the two table data separately? all i want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately ?
It doesn't matter what table is first... Simply:
<?php
$this->db->select('t1.name, t2.something, t3.another')
->from('table1 as t1')
->where('t1.id', $id)
->join('table2 as t2', 't1.id = t2.id', 'LEFT')
->join('table3 as t3', 't1.id = t3.id', 'LEFT')
->get();
?>
here is how it works:
suppose we have two tables namely student, library.
note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_id column
Write the the select query as follows, in the brackets write what are all the things you want
note:write as shown below don't put quotes to each single one put it on whole at once.
*note: suppose we want name, phone_no. from student table and book_name form library table.*
$this->db->select('name, phone_number, book_name');
Now write the from query and write one of the table name(No rule)
$this->db->from('student');
Now join this with the another table with join query
$this->db->join('library', 'students.std_id=library_std_id');
Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)
$this->db->where('std_id', 1);
$q= $this->db->get();
That's it it's done now you can print and check the result.
$this->db->join('comments', 'comments.id = blogs.id');
With this line you tell: search me inside comments all comments with id equal blogs.id.
Usually is something like that I think:
$this->db->join('comments', 'comments.blogs_id = blogs.id');
You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments.
Isn't important the position of first or second value
Hi this will work for joining two tables in codeIgnator.
$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
$this->db->from('chat');
$this->db->join('post', 'chat.id = post.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}
You can change to the query as you like do trail and error method to get your appropriate results.
This is my code for joint many tables as possible.
I have 3 tables professeurs,publications and support.
public function toutesdonnées(){
$this->db->select("*");
$this->db->from('publication');
$this->db->join('support', 'publication.idsup = support.idsup');
$this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}