Then I showed you how to retrive data from a database using manual witten codes..
TODAY my aim is to show you how we can save data of a form in a database..
Step 01 : Create the database and the table.
First what I need is a database.
Create a database called 'Data'.
Inside the database we created,create a table using following codes...
Create table infos(Id varchar(20) primary key,
Name varchar(20),
Age int);
Now the table and the database is ok....
Step 02: Create the Controller.
Now I need a controller.Save below set of codes as BioController.php in \protected\controllers directory.
Read the comment instructions carefully.
<?php
class BioController extends Controller{
public function actionCreate()
{
$model=new Bio; //Here I create a new model called Bio
if(isset($_POST['Bio']))
{
$model->attributes=$_POST['Bio'];
if($model->save()){
}
}
$this->render('create',array(
'model'=>$model,
));
}
}
?>
Step 03:Now I need my my model.Save the below set of codes as Bio.php in \protected\models directory.
<?php
class Bio extends CActiveRecord{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'infos';
}
/**
* @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('Id', 'required'),
array('Age', 'numerical', 'integerOnly'=>true),
array('Id, Name', 'length', 'max'=>20),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('Id, Name, Age', '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',
'Age' => 'Age',
);
}
/**
* 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,true);
$criteria->compare('Name',$this->Name,true);
$criteria->compare('Age',$this->Age);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
?>
Step 04:now I need to create my views.So creat a folder called Bio in \protected\views direcory.
Then save the below codes in it as create.php
<h1>Create Info</h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
Step 05:Then Save the below codes also as _form.php in \protected\views\Bio directory.
<?php
/* @var $this InfoController */
/* @var $model Info */
/* @var $form CActiveForm */
?>
<div >
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'bio-form',
'enableAjaxValidation'=>false,
)); ?>
<p >Fields with <span >*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div >
<?php echo $form->labelEx($model,'Id'); ?>
<?php echo $form->textField($model,'Id',array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->error($model,'Id'); ?>
</div>
<div >
<?php echo $form->labelEx($model,'Name'); ?>
<?php echo $form->textField($model,'Name',array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->error($model,'Name'); ?>
</div>
<div >
<?php echo $form->labelEx($model,'Age'); ?>
<?php echo $form->textField($model,'Age'); ?>
<?php echo $form->error($model,'Age'); ?>
</div>
<div >
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Step 05: That is all....
Go to the browser and type [path to your base directory]/index.php?r=Bio/create
You will see the below.....
Step 06:
Add few data into it.And see that database have saved them...Be happyyyyyyyy!!!!!