Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to Write a controller for get data from table 2 with id table 1
  • Hi everybody, I'm newbie fuel,
    I'm bulding a simply student's gradebook system. I only use 2 tables are students and student_gradebooks.
    table students(id, fullname, etc...)
    table: student_gradebooks(id, student_id, subject, grade).
    Problem is, I dont know how to create a controller to get data form table student_gradebooks when I had id student from table students.
    Anybody could send me some examples ? 
  • HarroHarro
    Accepted Answer
    Look into ORM.

    Create a model for your two tables, or use the 'oil refine fromdb' commandline command to generate them. Add the relation between the two (student has_many gradebooks, gradebook belongs_to student), and off you go.

    After that it will be as easy as

    $student = Model_Student::query()->where('id', '=', 1)->related('gradebook')->get_one();

    // show the students fullname
    echo $student->fullname;

    // show the subjects and grade
    foreach ($student->gradebook as $grade)
    {
        echo $grade->subject, $grade->grade;
    }

    Job done. ;-)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion