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
Related
I have five table name books,categories,publications,author,reviews.
in my app user can review the book after login. so reviews table stay empty until user post a review.
I am trying to run the flowing query in codeigniter model to get book details by category_id and it's working perfect when their review is exist otherwise it's return empty array. this is happening because of this $this>db>join('reviews','reviews.book_id = books.book_id'); condition return false
how can i show result even reviews table on condition is not match?
public function get_book_details($cat_id) {
$this->db->select('*');
$this->db->from('books');
$this->db->join('categories', 'categories.id = books.category_id');
$this->db->join('publications', 'publications.id = books.publication_id');
$this->db->join('author', 'author.id = books.author_id');
$this->db->join('reviews', 'reviews.book_id = books.book_id');
$this->db->where('categories.id', $cat_id);
$query = $this->db->get();
return $query->result();
}
Use LEFT JOIN to get the result if there are no matching associations are found
$this->db->join('reviews', 'reviews.book_id = books.book_id','LEFT');
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
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
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
I'm new to PHP/MySQL and super-new to CodeIgniter..
I have information in many MySQL tables. I want to retrieve it with JOIN where the tables primary keys are equal to $variable... How can I do it and get all the fields without the primary key field???
What I'm doing now is this (only two tables joined here):
function getAll($id) {
$this->db->select('*');
$this->db->from('movies');
$this->db->join('posters', 'movies.id= posters.id');
// WHERE id = $id ... goes here somehow...
$q = $this->db->get();
if ($q->num_rows() == 1) {
$row = $q->row();
$data = array(
'id' => $row->id,
'title' => $row->title,
'year' => $row->year,
'runtime' => $row->runtime,
'plotoutline' => $row->plotoutline,
'poster_url' => $row->poster_url
);
}
$q->free_result();
return $data;
id (PK), title, year, runtime and plotoutline are columns from the first table and poster_url is a field from the second table. The second table also contains an ID (PK) column that I don't want to Retrieve because I already have.
Jon is right. Here's an example:
$this->db->select('movies.id,
movies.title,
movies.year,
movies.runtime as totaltime,
posters.poster_url');
$this->db->from('movies');
$this->db->join('posters', 'movies.id= posters.id');
$this->db->where('movies.id', $id);
$q = $this->db->get();
This will return objects that have ->id, ->title, ->year, ->totaltime, and ->poster_url properties. You won't need the additional code to fetch the data from each row.
Don't forget, if the Active Record syntax gets a little unwieldy, you can use full SQL queries and get the same results:
$sql = "SELECT movies.id,
movies.title,
movies.year,
movies.runtime as totaltime,
posters.poster_url
FROM movies
INNER JOIN posters ON movies.id = posters.id
WHERE movies.id = ?"
return $this->db->query($sql, array($id))->result();
Both forms will ensure that your data is escaped properly.
CodeIgniter Active Record
Query Binding in CodeIgniter
An asterisk will return all the fields. To return a subset of these, i.e. all fields apart form the repeated id field, simply list the columns which you require rather than use '*'.
It is often a good idea to not use asterisk anyway. In the future of the app, someone may add a large field to the table which will be surplus to your requirements, and will slow your queries.
Simply put with method chaining:
$this->db->select('*')
->from('movies')
->join('posters', 'movies.id= posters.id')
->where('movies.id', $id)
->get();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();