简体   繁体   中英

NOT able to store data in database in yii

I am a learner in YII. I cant store the values in the datatbase, pls help me in this. I must store the values in the product table. the values are fetched but it is not stored in the DB. Thank you in advance Controller:

<?php

class ProductController extends Controller
{
    public function actionCreate()
    {
        $model=new CreateForm;
    // collect user input data
        if(isset($_POST['CreateForm']))
        {
            $model->attributes=$_POST['CreateForm'];
                        if($model->validate())
                        {
                           echo "HI";

                        }


        }
        // display the login form
        $this->render('create',array('model'=>$model));


    }
}
?>

Model:

<?php

class CreateForm extends CFormModel
{
    public $product_name;
    public $category_name;
    public $description;

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    public function tableName()
    {
        return 'products';
    }
    public function rules()
    {
        return array(
            array('product_name, category_name, description', 'required'),
           array('product_name', 'unique', 'className' => 'Product', 'attributeName' => 'product_name', 'message'=>'This product name is already in use'),
    array('product_name,category_name,desc', 'safe'),   
            );

    }

    public function attributeLabels()
    {
        return array(
            'product_name'=>'PRODUCT NAME',
                    'category_name'=>'CATEGORY',
                    'description'=>'DESCRIPTION'
        );
    }



}
?>

view:

<?php
$this->pageTitle=Yii::app()->name . ' - Create';
$this->breadcrumbs=array(
    'Create',
);
?>

<h1>CREATE</h1>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'create-form',
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,

)); 

?>


    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <div class="row">
        <?php echo $form->labelEx($model,'product_name'); ?>
        <?php echo $form->textField($model,'product_name'); ?>
        <?php echo $form->error($model,'product_name'); ?>
    </div>

    <div class="row">

        <?php
             echo $form->labelEx($model,'category_name');   
                $records = Category::model()->findAll();
               $list = CHtml::listData($records, 'category_id', 'category_name');
               echo $form->dropDownList($model,'category_name',$list,array ('prompt'=>'select '));
               echo $form->error($model,'category_name');
                ?>

    </div>

    <div class="row">
            <?php echo $form->labelEx($model,'description'); ?></br>
        <?php echo $form->textArea($model,'description',array('style' => 'height:100px;width:500px;','maxlength'=>500)); ?>
        <?php echo $form->error($model,'description'); ?>
    </div>

    <div class="row submit">
        <?php echo CHtml::submitButton('CREATE'); ?>
    </div>

<?php $this->endWidget(); ?>
</div><!-- form -->
 if($model->validate())
 {
      if(!$model->save()) // save it here!
      {
           // do stuff after successful save
      }
 }

UPDATE:

CFormModel does not have method save()

you need to extend your model from CActiveRecord

function saveInDb()
{
    $command = Yii::app()->db->createCommand(); // create a sql command and do insert here
    .
    . 
    .
}

Why not

if($model->validate())
{
   $model->save();
}

?

well, first of all, tf this is supposed to be a form based on table data you need to extend not from CFormModel but from CActiveRecord. second: for better readability and portability rewrite your tableName method

public function tableName()
    {
        return '{{products}}';
    }

Try This, Save function auto call validate form.

if($model->save()){
      echo 'add record success'.
}
else {
      echo 'failed'.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM