Skip to content

using conditions in a belongs_to, doesn't allow a model attribute #30

Open
@bbto

Description

@bbto

First of all, I know that the fields names don't meet some of the active record rules, especially because of the names, but as you know, active record should be flexible to allow this, and its awesome to find that php-ar does.

Well, thing is I have a Question model (obviously for storing questions) and a QuestionOption model (for the answers of a question)

The Question's model primary key is composed by two fields 'id_test_page' and 'question_index', and so is the QuestionOptions's foreign key.

My workaround to solve this was to use the conditions option of the belongs_to. For example:

static $belongs_to = array(
array( 'question',
'class' => 'Question',
'foreign_key' => 'id_test_page',
'conditions' => 'question.question_index = #{question_index}'));

But the #{question_index} wouldn't refer to the field question_index in this model. Do you have an Idea of how could i do this?

Below is the QuestionOption model as i'm using it right now, i had to create a function to retrieve the associated instance of the Question model.

// Class QuestionOption
class QuestionOption extends ActiveRecord\Model {
    static $table_name = "question_option";

    // QuestionOption belongs to an ImageData and a Question
    static $belongs_to = array(
        array(  'image_data',
                'class'       => 'ImageData',
                'foreign_key' => 'option_image'));
        // array(   'question',
        //      'class'       => 'Question',
        //      'foreign_key' => 'id_test_page',
        //      'conditions'  => 'question.question_index = #{question_index}'));

    public function question() {
        return Question::find_by_id_test_page_and_question_index($this->id_test_page, $this->question_index);
    }

} // end class

Thanks for your help guys!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions