There are two tables question and options…
question contains ‘question’ and ‘qid’
options contains ‘oid’, ‘qid’ and ‘optiontext’
I display a form where a person can type a question and then enter 1 option for it. The question should get stored into question table, option should get stored in option table
I made the form, defined the relations() in both the models
but the form gives me error saying "Property "Questions.optiontext" is not defined."
models/Questions.php
<?php
/**
* This is the model class for table "questions".
*
* The followings are the available columns in table 'questions':
* @property integer $qid
* @property string $question
*/
class Questions extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return Questions the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'questions';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('question', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('qid, question', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'options'=>array(self::HAS_MANY, 'options', 'qid')
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'qid' => 'Qid',
'question' => 'Question',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('qid',$this->qid);
$criteria->compare('question',$this->question,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
models/Options.php
<?php
/**
* This is the model class for table "options".
*
* The followings are the available columns in table 'options':
* @property integer $oid
* @property integer $qid
* @property string $optiontext
* @property integer $type
*/
class Options extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return Options the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'options';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('qid, optiontext, type', 'required'),
array('qid, type', 'numerical', 'integerOnly'=>true),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('oid, qid, optiontext, type', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'questions'=>array(self::BELONGS_TO, 'question', 'qid')
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'oid' => 'Oid',
'qid' => 'Qid',
'optiontext' => 'Optiontext',
'type' => 'Type',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('oid',$this->oid);
$criteria->compare('qid',$this->qid);
$criteria->compare('optiontext',$this->optiontext,true);
$criteria->compare('type',$this->type);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
views/questions/_form.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'questions-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'question'); ?>
<?php echo $form->textArea($model,'question',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->textArea($model,'optiontext',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'question'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
What am I missing here?