Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem while relating models
  • I basically have 2 models, questions, and objanswers. Question contains id, qtext and qtype. Objanswers has id, qid, otext. otext contains the answer. In the controller of questions, i want to display the actual question (i.e., qtext) and also the answer (i,e, otext) by doing a natural join of these 2 tables. In Controller_Questions class : public function action_view($id = null)
    {
    $data = Model_Objanswer::find()->related('otext')->get();
    $this->template->title = "Question";
    $this->template->content = View::factory('questions/view', $data);
    } These are the 2 model files : objanswer.php : class Model_Objanswer extends Orm\Model { protected static $_properties = array('id','qid','otext','correct');
    protected static $_primary_key = array('id');
    } question.php : class Model_Question extends Orm\Model
    {
    protected static $_properties = array('id','qtext','qtype');
    protected static $_primary_key = array('id'); protected static $_has_many = array(
    'otext' => array(
    'key_from' => 'qid',
    'model_to' => 'Model_Objanswer',
    'key_to' => 'qid',
    'cascade_save' => true,
    'cascade_delete' => false,
    )
    );
    }
    (since a question can have multiple answers) The view corresponding to the controller questions : <p>
    <strong>Qtext:</strong>
    <?php echo $question->qtext; ?></p>
    <p>
    <strong>Qtype:</strong>
    <?php echo $question->qtype; ?></p> <p>
    <strong>Answer:</strong>
    <?php #echo $question->otext; ?></p> <?php echo Html::anchor('questions/edit/'.$question->id, 'Edit'); ?> |
    <?php echo Html::anchor('questions', 'Back'); ?> I get the following error when action_view is invoked: Orm\UndefinedRelation [ Error ]: Relation "otext" was not found in the model. I'm really new to fuelphp, so I might be missing out on something obvious.
    What am I doing wrong?
  • Hi Karan, We are all new to Fuel :-)
    This is my current understanding of this ORM. You relate to a table not to a field within a table. Your table name should be questions not question.
    $data['question'] = Model_Objanswer::find()->related('questions')->get();
    

    You also don't need to declare your primary key if it is 'id' as this is the default.
  • @Karan Controller_Questions:
     public function action_view($id = null)
    {
      // Uncomment for lazy loading
      //$data['questions'] = Model_Question::find($id);
     
      // Uncomment for eagar loading
      //$data['questions'] = Model_Question::find($id, array('related' => array('objanswers')));
    
      $this->template->title = "Question";
      $this->template->content = View::factory('questions/view', $data);
     }
    
    Model_Objanswer :
    class Model_Objanswer extends Orm\Model {
    
     protected static $_properties = array('id','qid','otext');
     protected static $_belongs_to = array('questions');
    }
    
    Model_Question :
    class Model_Question extends Orm\Model {
    
     protected static $_has_many = array('objanswers' => array(
      'model_to' => 'Model_Objanswer',
      'key_from' => 'id',
      'key_to' => 'qid',
      'cascade_save' => true,
      'cascade_delete' => false,
     ));
    }
    
    questions/view.php
    <p>
     <strong>Qtext:</strong>
     <?php echo $questions->qtext; ?>
    </p>
    
    <p>
     <strong>Qtype:</strong>
     <?php echo $questions->qtype; ?>
    </p>
    
    <strong>Answers:</strong><br>
    <?php foreach($questions->objanswers as $objanswer): ?>
    <?php echo $objanswer->otext; ?><br>
    <?php endforeach ?>
    
    <?php echo Html::anchor('questions/edit/'.$questions->id, 'Edit'); ?> |
    <?php echo Html::anchor('questions', 'Back'); ?>
    

    Check out the docs on ORM - Relating Models http://fuelphp.com/docs/packages/orm/relating_models.html
  • Almost what PaulboCo said, 2 corrections to his models: Model_Objanswer :
    class Model_Objanswer extends Orm\Model {
     protected static $_properties = array('id','qid','otext');
     protected static $_belongs_to = array('questions' => array('key_from' => 'qid'));
    }
    
    You need to set the key from because it's expected to be "question_id". Model_Question :
    class Model_Question extends Orm\Model {
        protected static $_has_many = array('objanswers' => array('key_to' => 'qid'));
    }
    
    You only need to set the key_to because that's still expected to be "question_id", all the other values are like they're expected. Edit: Also next time: put your code between [ code] & [/ code] tags (without the space in there)

Howdy, Stranger!

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

In this Discussion