$data['question'] = Model_Objanswer::find()->related('questions')->get();
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'); ?>
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) It looks like you're new here. If you want to get involved, click one of these buttons!