Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Relating Models - where clause issue
  • Hello,

    I have a 'Category' ->  'Questions' -> 'Answer Options to Questions' schema with a 'has_many' relation property on model level for the questions.

    If it is an open question (like 'Why?' or 'What's your name?') it won't have any options store in the database.

    The code is this:

    $data['category']  =   Model_Category::query()
                    ->where('id', $id)
                    ->related('questions')
                    ->related('questions.answer_options')
                    ->where('questions.answer_options.is_active', 'YES')
                    ->get();

    Which gives me this query:

    SELECT ... FROM
     (...) AS `t0`
    LEFT JOIN
    `em_questions` AS `t1` ON (`t0`.`id` = `t1`.`indicator_id`)
    LEFT JOIN `em_question_answers` AS `t3` ON (`t1`.`id` =
    `t3`.`question_id`)
    WHERE `t3`.`is_active` = 'YES

    The problem is that this query leaves out all the questions that doesn't have answer options, I need the code to produce something like:

    SELECT ... FROM
     (...) AS `t0`
    LEFT JOIN
    `em_questions` AS `t1` ON (`t0`.`id` = `t1`.`indicator_id`)
    LEFT JOIN `em_question_answers` AS `t3`
    ON (`t1`.`id` =
    `t3`.`question_id`
    AND `t3`.`is_active` = 'YES')

    I tried placing the condition as a model property ($_conditions) but when I do the relation fuel just ignores the where clause.

    Any ideas?

    Thanks

  • HarroHarro
    Accepted Answer
    Ignoring conditions should be something that is fixed in the current release version, so if you're not on 1.6.1, I suggest you upgrade.

    As to your other question, I don't think you can currently add additional join clauses using chaining, please make a feature request for this at https://github.com/fuel/orm/issues, stating your use case.

Howdy, Stranger!

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

In this Discussion