我有一个MYSQL表的模型和一个用户可以用来输入特定值的表单。我遇到的问题是将表单文本输入值传递给模型以进行插入。如果我在模型中删除了我所需的规则,它将在表中输入一行但是所有空值。
这是
模型:
class QRCodes extends \CActiveRecord {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return 'qr_codes';
}
public function rules() {
return array(
array('qr_code_name,target_url,assigned_url','required'),
//array('enabled,site_id,user_id','numerical', 'integerOnly' => true),
array('qr_code_name,target_url,assigned_url,qr_comment','length','max'=>255),
array('qr_code_id,qr_code_name,target_url,assigned_url,qr_image,qr_file, qr_comment,enabled,site_id,user_id','safe','on'=>'search'),
);
}
public function relations() {
return array();
}
public function attributeLabels() {
return array(
'qr_code_id' => 'QR ID',
'qr_code_name' => 'QR Name',
'target_url' => 'Target URL',
'assigned_url' => 'Assigned URL',
'qr_image' => 'QR Image',
'qr_file' => 'QR File',
'qr_comment' => 'QR Comment',
'create_date' => 'Create Date',
'mod_date' => 'Mod Date',
'enabled' => 'Enabled',
'site_id' => 'Site ID',
'user_id' => 'User ID',
);
}
public function search() {
$criteria = new \CDbCriteria;
$criteria->compare('qr_code_id', $this->qr_code_id);
$criteria->compare('qr_code_name', $this->qr_code_name,true);
$criteria->compare('target_url', $this->target_url,true);
$criteria->compare('assigned_url', $this->assigned_url, true);
$criteria->compare('qr_image', $this->qr_image);
$criteria->compare('qr_file', $this->qr_file);
$criteria->compare('qr_comment', $this->qr_comment);
$criteria->compare('create_date', $this->create_date);
$criteria->compare('mod_date', $this->mod_date);
$criteria->compare('enabled', $this->enabled);
$criteria->compare('site_id', $this->site_id);
$criteria->compare('user_id', $this->user_id);
return new \CActiveDataProvider($this,array(
'criteria' => $criteria,
));
}
}
查看
use src\models\web_data\QRCodes;
/* @var $this DefaultController */
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'qrcodes-form',
'enableAjaxValidation' => false,
'type' => 'horizontal'
));
?>
<p class="help-block">
Fields with <span class="required">*</span> are required.
</p>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->textFieldRow($model, 'qr_code_name', array('class' => 'span5', 'maxlength' => 255, 'readonly' => !$model->isNewRecord)); ?>
<?php echo $form->textFieldRow($model, 'target_url', array('class' => 'span5', 'maxlength' => 255,'readonly' => !$model->isNewRecord)); ?>
<?php echo $form->textFieldRow($model, 'assigned_url', array('class' => 'span5', 'maxlength' => 255,'readonly' => !$model->isNewRecord)); ?>
<?php echo $form->textFieldRow($model, 'qr_comment', array('class' => 'span5', 'maxlength' => 255,'readonly' => !$model->isNewRecord)); ?>
<div class="form-actions">
<?php
$this->widget('bootstrap.widgets.TbButton', array(
'buttonType' => 'submit',
'type' => 'primary',
'label' => $model->isNewRecord ? 'Create' : 'Update',
));
?>
</div>
<?php
print_r($model->getErrors());
$this->endWidget();
?>
控制器
<?php
namespace admin\modules\coredataadministration\controllers;
use admin\modules\coredataadministration\components\Controller;
use src\models\web_data\QRCodes;
class QRController extends Controller {
/**
* (non-PHPdoc)
* @see CController::filters()
*/
public function filters() {
return parent::filters();
}
/**
* (non-PHPdoc)
* @see \components\admin\Controller::beforeAction()
*/
public function beforeAction($action) {
return parent::beforeAction($action);
}
public function actionIndex() {
$this->render('index');
}
public function actionCreate() {
$model = new QRCodes('CRUD');
if ($model->save()) {
$this->redirect($this->createUrl('index'));
}
$this->render('create',array('model' => $model));
}
}
我错过了什么?