无法在yii中将数据存储在数据库中

时间:2013-12-02 08:42:54

标签: php yii

我是YII的学习者。我不能将值存储在datatbase中,请帮助我。我必须将值存储在product表中。获取值但它不存储在DB中。先感谢您 控制器:

<?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));


    }
}
?>

型号:

<?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'
        );
    }



}
?>

视图:

<?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 -->

3 个答案:

答案 0 :(得分:1)

 if($model->validate())
 {
      if(!$model->save()) // save it here!
      {
           // do stuff after successful save
      }
 }

更新:

CFormModel没有方法save()

您需要从CActiveRecord

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

答案 1 :(得分:1)

为什么不

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

好吧,首先,如果这应该是基于表数据的表单,则需要不是从CFormModel而是从CActiveRecord扩展。 第二: 为了更好的可读性和可移植性,重写tableName方法

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

答案 2 :(得分:1)

试试这个,保存功能自动调用验证表单。

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