i have 3 tables-
users(id,name,dob) books(id,title,author) issuedbooks(id,book_id,student_id,issue_date) the relationship between user , book many many resulting in third table issuedbooks.
my models are-
class student extends datamapper{ var $table="students"; var $has_many=array( "books"=>array( "class"=>"book", "join_table"=>"issuedbooks", "join_self_as"=>"student", "join_other_as"=>"book", "other_field"=>"students" ) ); } class book extends datamapper{ var $table="books"; var $has_many=array( "students"=>array( "class"=>"student", "join_table"=>"issuedbooks", "join_self_as"=>"book", "join_other_as"=>"student", "other_field"=>"books" ) ); } this table issuedbooks has entry like-
id student_id book_id issue_date 1 2 1 2013-07-18 2 2 4 2013-07-16 3 1 5 2013-07-18 4 2 6 2013-07-18 now have find out books opted student id 2 , issue_date 2013-7-17.
i've tried, won't result.
$student=new student(); $student->get_by_id('2'); $student->books->include_join_fields()->get(); foreach($student->books $book): $book->where_join_field($student,'issue_date >',"2013-07-17")->get();
echo $book->title." ".$book->join_issue_date."<br />"; endforeach; please me out, going wrong?
here solution problem, it's simple guess.
, don't have create id column in many-to-many table (issuedbooks).
issuedbooks.book_id , issuedbooks.student_id must primary_keys.
class somemodel extends ci_model { public function getbooksbystudentidanddate($students_id, $date) { $students_id = (int) $students_id; $date = (date('y-m-d', strtotime($date)) === $date) ? $date : false; if( ! $students_id or ! $date) return array(); $this->db->where('issuedbooks.students_id', $students_id); $this->db->where('issuedbooks.issued_date', $date); $this->db->select(' users.name, books.title, issuedbooks.issued_date date '); $this->db->join('users','issuedbooks.students_id=users.id'); $this->db->join('books','issuedbooks.book_id=books.id'); $this->db->order_by('issuedbooks.issued_date', 'asc'); return $this->db->from('issuedbooks')->get()->result_array(); } }
Comments
Post a Comment