Inserting records through model class

Hello All,

I have created MVC for contact us page and database table also. Now I want to insert submitted records to table through ContactUs model class how can I do that. Please help me, I’m new bie;

Please see my class file here, code is generated by Gii and modified by me




<?php


/**

 * This is the model class for table "tbl_contact_us".

 *

 * The followings are the available columns in table 'tbl_contact_us':

 * @property integer $id

 * @property string $name

 * @property string $email

 * @property integer $phone

 * @property string $company

 * @property integer $business_type

 * @property integer $industry_type

 * @property string $subject

 * @property string $message

 * @property string $date_time

 * @property string $active

 * @property string $deleted

 * @property string $verifyCode

 */

class ContactUs extends CActiveRecord {


    public $verifyCode;


    /**

     * Returns the static model of the specified AR class.

     * @return ContactUs 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_contact_us';

    }


    /**

     * @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('name, email, phone, company, business_type, industry_type, subject, message', 'required'),

            array('phone, business_type, industry_type', 'numerical', 'integerOnly' => true),

            array('name, email, company', 'length', 'max' => 108),

            array('subject', 'length', 'max' => 216),

            array('message', 'length', 'max' => 1000),

            array('email', 'email'),

            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements()),

            // array('active, deleted', 'length', 'max' => 1),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('id, name, email, phone, company, business_type, industry_type, subject, message, date_time, active, deleted', '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',

            'name' => 'Name',

            'email' => 'Email',

            'phone' => 'Phone',

            'company' => 'Company',

            'business_type' => 'Business Type',

            'industry_type' => 'Industry Type',

            'subject' => 'Subject',

            'message' => 'Message',

            'date_time' => 'Date Time',

            'active' => 'Active',

            'deleted' => 'Deleted',

            'verifyCode' => 'Verification Code',

        );

    }


    /**

     * 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('name', $this->name, true);

        $criteria->compare('email', $this->email, true);

        $criteria->compare('phone', $this->phone);

        $criteria->compare('company', $this->company, true);

        $criteria->compare('business_type', $this->business_type);

        $criteria->compare('industry_type', $this->industry_type);

        $criteria->compare('subject', $this->subject, true);

        $criteria->compare('message', $this->message, true);

        $criteria->compare('date_time', $this->date_time, true);

        $criteria->compare('active', $this->active, true);

        $criteria->compare('deleted', $this->deleted, true);


        return new CActiveDataProvider($this, array(

                    'criteria' => $criteria,

                ));

    }


}



You should take a look at the blog demo that comes with Yii. It will show you how to populate your model based on a sumbitted form. Once your AR model ready, just call save() on it, and your data should be saved.

make with gii crud and analyse the code. you will see how it is done.

Thanks