I have a problem creating new records using active record.
I made a database connection to a MYSQL database which is working properly.
Next i made a user model class of the tbl_user table using gii tools.
<?php
/**
* This is the model class for table "tbl_user".
*
* The followings are the available columns in table 'tbl_user':
* @property integer $id
* @property string $email
* @property string $password
* @property string $name
* @property string $address
* @property double $height
* @property double $weight
* @property string $birth
* @property integer $sex
* @property string $registered_at
*/
class User extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return User 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 'tbl_user';
}
/**
* @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('email, password, name, address, height, weight, birth, sex, registered_at', 'required'),
array('sex', 'numerical', 'integerOnly'=>true),
array('height, weight', 'numerical'),
array('email, password, name', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, email, password, name, address, height, weight, birth, sex, registered_at', '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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'email' => 'Email',
'password' => 'Password',
'name' => 'Name',
'address' => 'Address',
'height' => 'Height',
'weight' => 'Weight',
'birth' => 'Birth',
'sex' => 'Sex',
'registered_at' => 'Registered At',
);
}
/**
* 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('id',$this->id);
$criteria->compare('email',$this->email,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('address',$this->address,true);
$criteria->compare('height',$this->height);
$criteria->compare('weight',$this->weight);
$criteria->compare('birth',$this->birth,true);
$criteria->compare('sex',$this->sex);
$criteria->compare('registered_at',$this->registered_at,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
In the SiteController i created a register action where i create a new user, insert the data and want to save it to the database using $user->save(); but i dont get the save option, i only get the functions defined in the user model class to show up.
public function actionRegister()
{
$model=new RegisterForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['RegisterForm']))
{
$model->attributes=$_POST['RegisterForm'];
// validate user input and redirect to the next page if valid
if($model->validate())
$user=new User;
$user->email = $model->email;
$user->password = $model->password;
$this->redirect(Yii::app()->user->returnUrl);
}
// display the register form
$this->render('register',array('model'=>$model));
}
So what am i missing here? Why isnt the ->save(); function showing up?