Codeigniter Many to Many Error - php

I have 3 tables ...
instructors, classes and instructor_teach
I want to display a list of classes that the instructor teaches
I have been using tutorials and the like to try and work this out so if my code is messy i do apologize.
Model...
function getMemberPrograms($id){
$this->db->select('*');
$this->db->from('instructor_teach');
$this->db->join('instructor_teach', 'instructors.id = instructor_teach.instructor_id');
$this->db->join('classes', 'instructor_teach.program_id = classes.id');
$this->db->where('instructor_teach.instructors_id', $id);
return;
}
In the controller i just have a print_r statement to see what the results are but at the moment i am getting
Error Number: 1066
Table/alias: 'instructor_teach' non unique
SELECT * FROM (`instructor_teach`, `classes`) JOIN `instructor_teach`
ON `instructors`.`id` = `instructor_teach`.`instructor_id` JOIN
`classes` ON `instructor_teach`.`program_id` = `classes`.`id` WHERE
`instructor_teach`.`instructors_id` = '1'
Filename: C:/wamp/www/fitness/application/models/members_model.php
Line Number: 16
what i want is an array that has all the information from the classes the instructor teaches so
instructor 1 teaches class 1, class 2
array would return
class 1.name
class 1.description
class 2.name
class 2.description
etc..
I hope this makes sense as i am still getting to terms with this
Many Thanks
Joe

I think you are joining the table instructor_teach to it self instructor_teach
take a look:
$this->db->from('instructor_teach');
$this->db->join('instructor_teach', 'instructors.id = instructor_teach.instructor_id');
I think it should be:
$this->db->from('instructor_teach');
$this->db->join('instructors', 'instructors.id = instructor_teach.instructor_id');
:)

$this->db->select('*');
$this->db->from('instructor_teach');
$this->db->join('instructor_teach', 'instructors.id = instructor_teach.instructor_id');
$this->db->join('classes', 'instructor_teach.program_id = classes.id');
$this->db->where('instructor_teach.instructors_id', $id);
First is you select to table instructor_teach then afterwards you want to join it again with table instructor_teach where in it will give you a error.
And one thing also if you can select only important columns from each tables is much more okay.
I think the query should look like this
$this->db->select('*');
$this->db->from('instructors');
$this->db->join('instructor_teach', 'instructors.id = instructor_teach.instructor_id');
$this->db->join('classes', 'instructor_teach.program_id = classes.id');
$this->db->where('instructor_teach.instructors_id', $id);

i don't know if this correct, but try to change the first join with instructor table
$this->db->join('instructor_teach', 'instructors.id = instructor_teach.instructor_id');
to
$this->db->join('instructor', 'instructors.id = instructor_teach.instructor_id');
try this
function getMemberPrograms($id){
$this->db->select('*');
$this->db->from('instructor_teach');
$this->db->join('instructor', 'instructors.id = instructor_teach.instructor_id');
$this->db->join('classes', 'instructor_teach.program_id = classes.id');
$this->db->where('instructor_teach.instructors_id', $id);
return true;
}

Related

sql command does not execute properly

I have a two tables in my database instructors and courses . I want to join them and for this reason wrote this code
$this->db->join('instructors', 'instructors.id = courses.instructor_id', 'left');
$query = $this->db->get_where('courses', array('courses_slug' => $slug));
return $query->row_array();
This code means:
SELECT * FROM `courses` LEFT JOIN `instructors` ON `instructors`.`id` = `courses`.`instructor_id` WHERE `courses_slug` = 'abituriyent-hazirligi'
But when I write this code to check:
$data['courses'] = $this->popular_courses_model->get_popular_courses($slug);
echo $data['courses']['id'];
die();
It writes the instructors id, not id of the course. Where can be the problem? Thanks in advance.
You are joining two table with columns of the same name ('id'). You need to be specific in your select for the columns and rename ('AS') if necessary.
select courses.id as course_id, instructor.id as instructor_id, ...
When using joins you should explicitly call out what columns you want returned like:
$select = "c.id, c.name, c.instructor_id, i.name instructor_name";
return $this->db->select($select)
//equivalent to "instructors as i"
->join('instructors i', 'i.id = c.instructor_id', 'left')
->where('c.courses_slug', $slug)
//equivalent to "courses as c"
->get('courses c')->row_array();

Cannot get relation in Laravel pivot tables

I want to build relations between three tables in laravel. Currently i have three models
Classroom,
public function subjects(){
return $this->belongstoMany('Subject','subject_section_classroom');
}
Section,
Subject
My Tables are
classrooms(id, name)
sections(id, name)
subjects(id, name)
subject_section_classroom( id, classroom_id, section_id, subject_id)
In my classroomsController I have
public function assignsubjects($class_id, $section_id){
$classroom = Classroom::find($class_id);
$section = Section::find($section_id);
$subjects = Subject::lists('name','id');
$selected_subjects = $classroom->subjects()->where('section_id', '=', 1);
$subjects = Subject::lists('name','id');
return view('assignedit', compact('classroom','section','subjects', 'selected_subjects'));
}
But I can't get the selected_subjects from above relation. And when I tried to get the sql of the above query (with ->toSQL()), I get
`"select * from `myschool_subjects` inner join `myschool_subject_section_classroom` on `myschool_subjects`.`id` = `myschool_subject_section_classroom`.`subject_id` where `myschool_subject_section_classroom`.`classroom_id` = ? and `section_id` = ?"`
I can't figure what I am doing wrong here.
Please Help.
I think your problem is coming from
php
$selected_subjects = $classroom->subjects()->where('section_id', '=', 1);
Change it to:
php
$selected_subjects = $classroom->subjects()->where('section_id', '=', 1)->get();

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();

codeigniter join 2 table data

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;
}

join 3 tables in mysql codeigniter

I have 3 tables in my database :-
tbl_roles(role_id,role_name);
tbl_users(id,role_id,username,email,password);
tbl_tickets_replies(id,ticket_id,user_id,role_id,comments)
role_id, id, id are primary keys of corresponding tables.
i need :-
username from tbl_users.
role_name from tbl_roles.
comments from tbl_tickets
where ticket_id from tbl_tickets_replies = $ticket_id coming as a parameter.
My Model Function is :-
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->from('tbl_tickets_replies');
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
$comments = $this->db->get('tbl_tickets_replies');
return $comments;
}
this is showing database error i.e., I am doing wrong query.
I want to ask how can I join three tables to fetch data from 3 different tables
This error is showing :-
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'tbl_tickets_replies'
SELECT tbl_tickets_replies.comments, tbl_users.username,
tbl_roles.role_name FROM (tbl_tickets_replies,
tbl_tickets_replies) JOIN tbl_users ON tbl_users.id =
tbl_tickets_replies.user_id JOIN tbl_roles ON
tbl_roles.role_id=tbl_tickets_replies.role_id WHERE
tbl_tickets_replies.ticket_id = '6'
Filename: C:\wamp\www\local.helpdesk.com\bonfire\codeigniter\database\DB_driver.php
Line Number: 330`
You are referring to tbl_tickets_replies twice.
Try this:
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,
tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
return $this->db->get('tbl_tickets_replies');
}
For complex queries, i prefer using the plain SQL as follows.
$sql = "SELECT....";
$q = $this->db->query($sql);
Btw, try removing the table name from db->get function
$comments = $this->db->get(); //change this
Join with condition.
$this->db->select('*'); $this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->where(array('category.id' => 10)); $query =
$this->db->get();

Categories