CodeIgniter Forums
DMZ (Datamapper) quick query question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: DMZ (Datamapper) quick query question (/thread-31935.html)



DMZ (Datamapper) quick query question - El Forum - 07-07-2010

[eluser]kungpoo[/eluser]
I love the idea of what the Datamapper could do for my projects. I'm trying to get my head around a very basic many-to-many join using the tables students, courses_students and courses.

The code to list all courses for a student that I am using is as follows -

Code:
$s = new Student();
$s->get();

foreach ($s->course->get() as $c) {
    echo $c->name;
}

$this->output->enable_profiler(TRUE);

My first question was related to what the profiler shows is happening to the DB -
Code:
SELECT * FROM `students` LIMIT 1
Code:
SELECT * FROM (`students`)
Code:
SELECT * FROM `courses` LIMIT 1
Code:
SELECT `courses`.*
FROM (`courses`)
LEFT OUTER JOIN `courses_students` courses_students ON `courses`.`id` = `courses_students`.`course_id`
WHERE `courses_students`.`student_id` = 1
Are all of these SELECT statements meant to be there? Should I be doing something differently?

Sorry for my ignorance! I seem to just be the to many stuff that is giving me grief. Thanks.


DMZ (Datamapper) quick query question - El Forum - 07-07-2010

[eluser]kungpoo[/eluser]
This seems to be more like what I want

Code:
$s = new Student($id);
$s->course->select('name');
$s->course->get_iterated();

foreach ($s->course as $c) {
    echo $c->name;
}

but still getting 2 select * limit 1 extra queries?
Code:
SELECT * FROM `students` LIMIT 1

SELECT *
FROM (`students`)
WHERE `students`.`id` = 1

SELECT * FROM `courses` LIMIT 1

SELECT `courses`.`name`
FROM (`courses`)
LEFT OUTER JOIN `courses_students` courses_students ON `courses`.`id` = `courses_students`.`course_id`
WHERE `courses_students`.`student_id` = 1



DMZ (Datamapper) quick query question - El Forum - 07-07-2010

[eluser]kungpoo[/eluser]
OK I didn't read around this enough... Sorry for wasting everyone's time!

http://www.overzealous.com/dmz/pages/prodcache.html