Many to many database association in codeigniter - php

I am creating a sort of card collection programme with codeigniter and ion_auth for authentication. A user should be able to add a card to his collection.
So i have a users table and a cards table and a junction/bridge table.
users
ID
NAME
and
cards
ID
NAME
and
users_cards
ID
USER_ID
CARD_ID
I want to accomplish a SELECT statement that retrieves the cards from a users through user_cards.
$this->db->select('*');
$this->db->from('cards');
$this->db->join('users_cards', 'users.id = users_cards.user_id', 'inner');
$this->db->join('users', 'users_cards.user_id = users.id', 'inner');
$query = $this->db->get();
return $query->result_array();
I cant seem to get a grasp at the concept of these joins. Can someone please help me?

Looks like the join statement is missing the reference to the 'cards' table.
$this->db->select('*');
$this->db->from('users_cards');
$this->db->join('users', 'users.id = users_cards.user_id', 'inner');
$this->db->join('cards', 'cards.id = users_cards.card_id', 'inner');
$query = $this->db->get();
return $query->result_array();
The idea is that you're trying to get a relationship going between users_cards and the other two tables by referencing their relevant ids

Since you didn't joined users table you can't join based on the users.id reference. So you need to change
$this->db->join('users_cards', 'users.id = users_cards.user_id', 'inner');
to
$this->db->join('users_cards', 'cards.id = users_cards.card_id', 'inner');

Related

How to get users and profile table data in codeigniter

I have two tables: one is users and other is user_profile and in users table suppose 20 row and user_profile table only two rows.
How could I get all user which has profile or whom has no profile in codeingniter?
You can use join as below
Note : user_id is primary key of users table
$this->db->select('*');
$this->db->join('user_profile up', 'up.user_id = u.user_id', 'left');
$this->db->get('users u')->result_array();
To get the users with profile, use INNER JOIN
public function users_profile(){
$this->db->select('users.*, user_profile.column1 as column1, user_profile.column2 as column2');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'inner');
$query = $this->db->get('users');
return $query->result_array();
}
To get all users with or without profile, use LEFT JOIN
public function all_users(){
$this->db->select('users.*, user_profile.column1 as column1, user_profile.column2 as column2');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'left');
$query = $this->db->get('users');
return $query->result_array();
}
Rememer to make user_id the PRIMARY KEY for both tables

codeigniter, join table properties overrides other properties

I'm working on a project using codeigniter 3 and I have a following query
$this->db->select("*");
$this->db->from("questions");
$this->db->join('users', 'users.id = questions.user_id');
$this->db->where('article_id', $articleId);
$this->db->order_by('questions.id', 'DESC');
$query = $this->db->get();
return $query->result();
// query
// SELECT * FROM `questions` JOIN `users` ON `users`.`id` = `questions`.`user_id` WHERE `article_id` = 18 ORDER BY `questions`.`id` DESC
Currently, the id property from users table overrides the questions id one. The question's id is crucial.
I worry, I will have to write a custom query, which I am not really good at.
Try to do this as follow:
$this->db->select("*, questions.id as question_id");
It's obvious that properties with the same name will be overriden. That's why you should assign to them 'unique' names in select statement.
You can specifically mention the ids with alies like -
$this->db->select("questions.id as question_id, users.id as user_id, questions.*, users.*");
$this->db->from("questions");
$this->db->join('users', 'users.id = questions.user_id');
$this->db->where('article_id', $articleId);
$this->db->order_by('questions.id', 'DESC');
$query = $this->db->get();
return $query->result();
This will give you specific columns named with question_id, user_id, questions table's all columns, users table's all columns
Or another better solution to use specific column names in select with alies if required.

How to get two id data from same table using codeigniter join funtion

I have table call jobs and i need to get jobs.user_iduser and jobs.adminId from same table call user
I try this code it output only one user data only.
$this->db->from('jobs');
$this->db->join('user', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('user', 'user.iduser = jobs.adminId', 'left');
then i try like this it output only one user data only.
$this->db->from('jobs');
$this->db->join('user', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('user as admin', 'admin.iduser = jobs.adminId', 'left');
Is there any way to achieve this?
You can try this.
$this->db->select('u.iduser as uiduser, ua.iduser as uaiduser and so on');
$this->db->from('jobs');
$this->db->join('user u', 'u.iduser = jobs.user_iduser', 'left');
$this->db->join ('user ua', 'ua.iduser = jobs.adminId', 'left');
In your case since the user joined in the first join is being replaced by the second join. So you need to try aliasing the tables.
Try this:
$this->db->from('user');
$this->db->join('jobs', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('jobs', 'user.iduser = jobs.adminId', 'left');
I don't exactly know the data in tables but left join in SQL means:
Select all records from Table A (user in above example),
along with records from Table B (jobs in above example) for
which the join condition is met (if at all). Ref
So, since you have said only one user data is being returned, with jobs being the left table I am assuming the table jobs contains only one user data. Let me know if you don't follow me.

How to get limited data parameter from joined table in codeigniter?

Hello i want to join three tables but it fetch all data from user table, i just want firstname and lastname parameter in that
Here is my join query. i want something like join(user.firstname).
$this->db->select('*');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$query = $this->db->get();
return $query->result();
You can do something like this:
$this->db->select('post.*, source.*, user.firstname, user.lastname');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$query = $this->db->get();
return $query->result();
The table.* indicates that you want all the fields from that table.
change $this->db->select('*');
to $this->db->select('user.firstname, user.lastname');
You can define that in the select:
$this->db->select(['post.id', 'post.title', 'post.description', 'user.firstname']);
Take a look at this answer https://stackoverflow.com/a/15402766/1897484

Query for getting values from multiple tables

I have a table video with fields videoid, genre(int-foreign key), language(int-foreign key) etc. I want to get the value of genre from genre table and language from movie_languages table.
The structure of tables are given below:
video
genre
movie_languages
How can I join these 3 tables to get the language and genre related to each videos in video table. Also when user didn't select genre/language in the form, value 0 will be inserted to the table. Will this affect the query. I am using codeingiter and I tried with the following query and is not working.
$this->db->select('video.*,movie_languages.language,genre.genre');
$this->db->join('genre', 'video.genre = genre.id');
$this->db->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();
Please help me.
Thanks in advance.
It will be, you need a left join in this case
Try this
$query = $this->db
->select('v.*,ml.language,g.genre')
->from('video as v')
->join('movie_languages AS ml', 'v.language = ml.id', 'left outer')
->join('genre AS g', 'v.genre = g.id', 'left outer')
->get();
try this
$this->db->select('video.*,movie_languages.language,genre.genre')
->from('video')
->join('genre', 'video.genre = genre.id')
->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();

Categories