I do not understand why I cannot add new record to my database. I want to have following scenario: user log in and run form (after auth). He should answer for few questions and send data to database (Sur table). Sur table stores id, id_user and answers for the questions. I cannot do this! Still action displays empty fields in the view form and it does not remember user id. I would like to check if user has filled this form. If yes - he should receive appropriate message instead of form running. It seems to be simple problem, but I cannot deal with it! I think that controller requires some new code. Could you look in this problem?
model Sur.php
* @property string $id
* @property string $user_id //id of user from Users table
* @property integer $quest1
* @property integer $quest2
* The followings are the available model relations:
* @property Users $user
class Sur extends CActiveRecord
'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
actionAddrec in SurContraller.php
public function actionAddrec()
$model=new Sur;
if(isset($_POST['Sur']))
$model->attributes=$_POST['Sur'];
$this->render('addrec',array(
'model'=>$model,
));
public function actionAddrec()
$model=new Sur;
if(isset($_POST['Sur']))
$model->attributes=$_POST['Sur'];
$this->render('addrec',array(
'model'=>$model,
));
use $model->save();
public function actionAddrec()
$model=new Sur;
if(isset($_POST['Sur']))
$model->attributes=$_POST['Sur'];
model->save(); //note
$this->render('addrec',array(
'model'=>$model,
));
Thank you for your time. I have added $model->save();. It helped. But it appears a new problem. Now if I run sur/addrec website, before form filling, data are sended to database with 0 value for quest1, guest2, etc.:
id|user_id|done|quest1|quest2|quest3
14|2|0|0|0
When I fill form for example by 3|2|5 and click Save, in database I have second record:
15|2|3|2|5
I do not want to have first record in database, because I will check if user has already filled the form. If yes, he receive message: "You have already filled form".
What can be wrong?
After adding
array('quest1, quest2, quest3', 'required'),
to the rules in the model, first record does not add to database. But form is opened with errors:
Please fix the following input errors:
quest1 cannot be blank.
Why requirements are checked before click on button Save?