i try with this code $model->attributes=$_POST[‘Student’];
StudentController.php
<?php
class StudentController extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Student;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Student']))
{
$model->attributes=$_POST['Student'];
//=======================================================Image Upload======================================================================//
$model->student_image=CUploadedFile::getInstance($model,'student_image');
if($model->save()){
$model->student_image->saveAs(Yii::app()->basePath.'/upload/'.$model->student_image);
$this->redirect(array('view','id'=>$model->student_id));
}}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Student']))
{
$model->attributes=$_POST['Student'];
//===================================================If Already Exist File======================================================================//
$student_image_file = CUploadedFile::getInstance($model,'student_image');
if ( (is_object($student_image_file) && get_class($student_image_file)==='CUploadedFile'))
$model->student_image = $student_image_file;
//===============================================================================================================================================//
if($model->save()){
if (is_object($student_image_file))
$model->student_image->saveAs(Yii::app()->basePath.'/upload/'.$model->student_image);
$this->redirect(array('view','id'=>$model->student_id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
/**
* Lists all models.
*/
// public function actionIndex()
// {
// $dataProvider=new CActiveDataProvider(‘Student’);
// $this->render(‘index’,array(
// ‘dataProvider’=>$dataProvider,
// ));
// }
/**
* Manages all models.
*/
public function actionIndex()
{
$model=new Student('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Student']))
$model->attributes=$_GET['Student'];
$this->render('index',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer $id the ID of the model to be loaded
* @return Student the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
$model=Student::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param Student $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='student-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
public function actionDynamicstates()
{
$sql = "SELECT state_name FROM state ".
"WHERE country_id = :country_id";
$command = Yii::app()->createCommand($sql);
$command->bindValue(':country_id', $_POST['country_id'], PDO::PARAM_INT);
$data = $command->query();
$data = CHtml::listData($data,'state_id','state_name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
}
student/_form.php
<?php
/* @var $this StudentController */
/* @var $model Student */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'student-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>15,'maxlength'=>15)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'student_fname'); ?>
<?php echo $form->textField($model,'student_fname',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'student_fname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'student_lname'); ?>
<?php echo $form->textField($model,'student_lname',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'student_lname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'student_dob'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'name' => 'student_dob',
'attribute' => 'student_dob',
'model'=>$model,
'options'=> array(
'dateFormat' =>'yy-mm-dd',
'altFormat' =>'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'appendText' => 'yyyy-mm-dd',
),
));
?>
<?php echo $form->error($model,'student_dob'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'country'); ?>
<?php
echo $form->dropDownList($model,'country_id', Country::model()->getCountryOptions());
// array(
// ‘ajax’ => array(
// ‘type’ => ‘POST’,
// ‘url’ => CController::createUrl(‘mymodelname/dynamicStates’),
// ‘update’ => ‘#MyModelName_’.‘state_id’
// )
// )
// );
?>
<?php echo $form->error($model,'CountryID'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state'); ?>
<?php echo $form->dropDownList($model,'state_id', State::model()->getStateOptions());?>
<?php echo $form->error($model,'state_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'city_id'); ?>
<?php echo $form->dropDownList($model,'city_id', City::model()->getCityOptions()); ?>
<?php echo $form->error($model,'city_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'gender'); ?>
<?php echo $form->dropdownList($model,'gender',array('1'=>'M','2'=>'F')); ?>
<?php echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email_address'); ?>
<?php echo $form->textField($model,'email_address',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'email_address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'contact_number'); ?>
<?php echo $form->textField($model,'contact_number',array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->error($model,'contact_number'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'student_image'); ?>
<?php echo $form->fileField($model, 'student_image'); ?>
<?php echo $form->error($model, 'student_image'); ?>
</div>
<?php if($model->isNewRecord!='1'){ ?>
<div class="row">
</div>
<?}?>
<div class="row">
<?php echo $form->labelEx($model,'package_id'); ?>
<?php echo $form->textField($model,'package_id'); ?>
<?php echo $form->error($model,'package_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'package_purchase_date'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'name' => 'package_purchase_date',
'attribute' => 'package_purchase_date',
'model'=>$model,
'options'=> array(
'dateFormat' =>'yy-mm-dd',
'altFormat' =>'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'appendText' => 'yyyy-mm-dd',
),
));
?>
<?php echo $form->error($model,'package_purchase_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'student_status'); ?>
<?php echo $form->textField($model,'student_status'); ?>
<?php echo $form->error($model,'student_status'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I solve problem but i can’t see uploaded image problem in baseUrl and basePath
any other suggestion for image upload time of updation of form