Hi i am new for yii. I have created the form,in this form there is two image fields these two fields are optional we submit the form with or with out giving this image field.i have problem to save the image in data base.The image are stored in folder but not stored in data base how to solve this problem can anyone suggest me.
Here is my code.
Model:
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('myv_apartment_door_no, myv_apartment_sq_feet, myv_apartment_total_sq_feet, myv_apartment_carpet_sq_feet, myv_apartment_no_of_bhk, myv_apartment_no_of_bath, myv_apartment_no_of_balcony, myv_apartment_rate, myv_sales_status_id, myv_progress_status_id, myv_sales_crm_id, myv_support_crm_id, myv_apartment_vacantflag', 'required'),
array('myv_apartment_rate', 'numerical'),
array('myv_apartment_door_no, myv_apartment_car_parking_lot_no', 'length', 'max' => 10),
array('myv_apartment_sq_feet, myv_apartment_total_sq_feet, myv_apartment_carpet_sq_feet, myv_apartment_no_of_bhk, myv_apartment_no_of_bath, myv_apartment_no_of_balcony, myv_sales_status_id, myv_progress_status_id, myv_sales_crm_id, myv_support_crm_id', 'length', 'max' => 11),
array('myv_apartment_floor_plan_image, myv_apartment_3d_plan_view', 'length', 'max' => 75),
array('myv_apartment_apartment_title', 'length', 'max' => 60),
array('myv_apartment_vacantflag', 'length', 'max' => 3),
array('myv_apartment_floor_plan_image, myv_apartment_3d_plan_view', 'file', 'types' => 'jpg, gif, png', 'safe'=>true, 'allowEmpty' => true, 'on'=>'insert'),
// array('myv_apartment_floor_plan_image, myv_apartment_3d_plan_view', 'file', 'types' => 'jpg, gif, png', 'allowEmpty' => true, 'on'=>'update'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('myv_apartment_id, myv_apartment_door_no, myv_apartment_sq_feet, myv_apartment_total_sq_feet, myv_apartment_carpet_sq_feet, myv_apartment_no_of_bhk, myv_apartment_no_of_bath, myv_apartment_no_of_balcony, myv_apartment_car_parking_lot_no, myv_apartment_floor_plan_image, myv_apartment_3d_plan_view, myv_apartment_rate, myv_apartment_apartment_title, myv_sales_status_id, myv_progress_status_id, myv_sales_crm_id, myv_support_crm_id, myv_apartment_vacantflag', 'safe', 'on' => 'search'),
);
}
Controller:
public function actionCreate() {
$model = new MyvApartment;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['MyvApartment'])) {
$rnd = rand(0, 9999); // generate random number between 0-9999
$uploadedFile = CUploadedFile::getInstance($model, 'myv_apartment_floor_plan_image');
if(!empty($uploadedFile)){
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->myv_apartment_floor_plan_image = $fileName;
}
$uploadedFile2 = CUploadedFile::getInstance($model, 'myv_apartment_3d_plan_view');
if(!empty($uploadedFile2)){
$fileName2 = "{$rnd}-{$uploadedFile2}"; // random number + file name
$model->myv_apartment_3d_plan_view = $fileName2;
}
$model->attributes = $_POST['MyvApartment'];
// echo "<pre>";
// print_r($model);
// die();
if ($model->save()){
if(isset($fileName)){
$uploadedFile->saveAs(Yii::app()->basePath . '/../images/apartmentfloorimage/' . $fileName); // image
}
if(isset($fileName2)){
$uploadedFile2->saveAs(Yii::app()->basePath . '/../images/apartmentfloorimage/' . $fileName2); // image
}
$this->redirect(array('view', 'id' => $model->myv_apartment_id));
}
}
$this->render('create', array(
'model' => $model,
));
}
view:
<<?php
/* @var $this MyvApartmentController */
/* @var $model MyvApartment */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'myv-apartment-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'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,'myv_apartment_door_no'); ?>
<?php echo $form->textField($model,'myv_apartment_door_no',array('size'=>10,'maxlength'=>10)); ?>
<?php echo $form->error($model,'myv_apartment_door_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_sq_feet'); ?>
<?php echo $form->textField($model,'myv_apartment_sq_feet',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'myv_apartment_sq_feet'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_total_sq_feet'); ?>
<?php echo $form->textField($model,'myv_apartment_total_sq_feet',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'myv_apartment_total_sq_feet'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_carpet_sq_feet'); ?>
<?php echo $form->textField($model,'myv_apartment_carpet_sq_feet',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'myv_apartment_carpet_sq_feet'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_no_of_bhk'); ?>
<?php echo $form->textField($model,'myv_apartment_no_of_bhk',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'myv_apartment_no_of_bhk'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_no_of_bath'); ?>
<?php echo $form->textField($model,'myv_apartment_no_of_bath',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'myv_apartment_no_of_bath'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_no_of_balcony'); ?>
<?php echo $form->textField($model,'myv_apartment_no_of_balcony',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'myv_apartment_no_of_balcony'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_car_parking_lot_no'); ?>
<?php echo $form->textField($model,'myv_apartment_car_parking_lot_no',array('size'=>10,'maxlength'=>10)); ?>
<?php echo $form->error($model,'myv_apartment_car_parking_lot_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_floor_plan_image'); ?>
<?php echo CHtml::activeFileField($model, 'myv_apartment_floor_plan_image'); ?>
<?php echo $form->error($model,'myv_apartment_floor_plan_image'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_3d_plan_view'); ?>
<?php echo CHtml::activeFileField($model, 'myv_apartment_3d_plan_view'); ?>
<?php echo $form->error($model,'myv_apartment_3d_plan_view'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_rate'); ?>
<?php echo $form->textField($model,'myv_apartment_rate'); ?>
<?php echo $form->error($model,'myv_apartment_rate'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_apartment_title'); ?>
<?php echo $form->textField($model,'myv_apartment_apartment_title',array('size'=>60,'maxlength'=>60)); ?>
<?php echo $form->error($model,'myv_apartment_apartment_title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_sales_status_id'); ?>
<?php echo $form->dropDownList($model,'myv_sales_status_id', MyvStatus::items('Sales')); ?>
<?php echo $form->error($model,'myv_sales_status_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_progress_status_id'); ?>
<?php echo $form->dropDownList($model,'myv_progress_status_id', MyvStatus::items('Progress')); ?>
<?php echo $form->error($model,'myv_progress_status_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_sales_crm_id'); ?>
<?php echo $form->dropDownList($model,'myv_sales_crm_id', MyvBuilderuser::items('Crm')); ?>
<?php echo $form->error($model,'myv_sales_crm_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_support_crm_id'); ?>
<?php echo $form->dropDownList($model,'myv_support_crm_id', MyvBuilderuser::items('Crm')); ?>
<?php echo $form->error($model,'myv_support_crm_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'myv_apartment_vacantflag'); ?>
<?php echo $form->dropDownList($model,'myv_apartment_vacantflag',array('Yes' => 'Yes', 'No' => 'No')); ?>
<?php echo $form->error($model,'myv_apartment_vacantflag'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->