I know there have been quite a few topic about this, but none of those helped me so bear with me.
I’m very new to Yii and making a simple application that allows a person to take a test. The participant enters some personal info on the first page, presses a button to start the test and gets a number of multiple-choice questions. Each question has it’s own page with a stateful form with a “next” button.
Simplified I have the following:
model
/**
* This is the model class for table "test_answer".
*
* The followings are the available columns in table 'test_answer':
* @property integer $id
* @property integer $test
* @property integer $question
* @property integer $answer
* @property integer $correct
* @property integer $faulty
* @property string $comment
* @property integer $time_spent
*
* The followings are the available model relations:
* @property Question $question0
* @property Answer $answer0
* @property Test $test0
*/
class TestAnswer extends CActiveRecord
{
....
/**
* @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('test, question, answer, correct, faulty, comment, time_spent', 'required'),
array('test, question, answer, correct, faulty, time_spent', 'numerical', 'integerOnly'=>true),
array('comment', 'length', 'max'=>1500),
);
}
....
}
If I understand it correctly the fact that comment is mentioned in the rules means it’s a safe attribute.
Controller snippet
.....
if(isset($_POST['Answer']))
{
.....
$testAnswers = $this->getPageState("testAnswers", array());
$testAnswer = new TestAnswer;
$moreValues = end($testAnswers);
echo "<br/>test3 | " . $moreValues['comment'] . "|";
$testAnswer->attributes = $moreValues;
// $testAnswer->setAttributes($moreValues, false);
// $testAnswer->comment = $moreValues->comment;
echo "<br/>test4 | " . $testAnswer->comment . "|";
....
$newTA = new TestAnswer;.
$newTA->comment = 'blabla';
array_push($testAnswers, $newTA);
$this->setPageState("testAnswers", $testAnswers);
$this->render('newTestPart2',array('model'=>$newTA,));
return;
}
....
This is basically the part that handles the answer and sets up the next question. Test3 returns "blabla", but Test4 returns nothing
Can someone explain to me why the comment property on TestAnswer is not set by the following line?
$testAnswer->attributes = $moreValues;