Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
What's the best way for set DB query builder results to ORM\Model?
  • Hello.

    ---------------
    <Table>
    ---------------
    tbl_schedule_201306s
    tbl_users
    tbl_schedule_type

    (1) Schedule has a user_id and schedule_type_id.
    (2) Schedule has a $_belongs_to property (It contains user and schedule_type relation).
    (3) Both user and schedule type Models are not have any relation property with schedule.
    (4) Get columns "schedule.*", "schedule_type.name" and "user.name" To show View.
    (5) All tables have "entry_time" column.

    --------------
    <Controller>
    --------------
    $schedules = \DB::select('SC.*')
                           ->from( array('schedule_201306s'. 'SC') )
                           ->join( array('users', 'US'), 'INNER')
                           ->on('US.id', '=', 'SC.user_id')

                           ->join( array('schedule_types', 'ST'), 'INNER')
                           ->on('ST.id', '=', 'SC.schedule_type_id')

                          ->as_object('\Schedule\Model_Schedule')
                          ->execute();

    ----------------
    <Trouble>
    ----------------
    (1)
    Results contain "_data_relations" automatically.
    But it's not all relations. "schedule_type" is contained, but "user" is not.
    Why did not user contain?

    (2)
    I think there is some possiblity what MySql not returns user column.
    So I changed select method like:

    select('*')

    Then 'schedule.entry_time' is overrited by other table's same column data.
    I add column to "select()" method like:

    select('SC.*', array('US.name',  'user_name'), array('SCT.name', 'schedule_type_name'))

    But result relation data contains "schedule_yupe_name".
    Is it neccesaly?

    What's I wrong?
    Please tell me.

  • Sorry, I found what related model is created when access by property.

    $schedule = \DB....                 <= Related model is not contained.
    foreach ($schedules as $schedule) {

    $schedule->user->name  <= Related model User Created!

    }

    It was solved.
  • I dumped the SQL by DB::last_query method contains join queried.
    Why result which is not accessed by it's property don't contain relation datas?
    Where are they saved?
  • If you feed data into a Model object, you're supposed to do the hydration yourself, the ORM model does not contain the logic to disect the result of a join, and automatically create related object, it does not know how to split the records.

    This is why ORM queries use aliases, so the field prefix in the result can be used to map the field back to the table, and because the ORM knows which relations you have included, in knows which related model objects it has to instantiate.

    It does not have any knowledge of related table names.

Howdy, Stranger!

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

In this Discussion