我想避免在存储在字段上的数据与db上的数据相同时保存数据库(以避免重复),在yii中,我使用gii制作的表单,我正在制作一个renderpartial另一个视图,所以你可能会在视图动作中看到代码,它完全正常,这里是代码。
控制器/动作(视图)
public function actionView($id)
{
$dec=new DecretoCaracterizacion;
if(isset($_POST['DecretoCaracterizacion']))
{
$error='error';
$model=DecretoCaracterizacion::model()->findAll();
$dec->attributes=$_POST['DecretoCaracterizacion'];
if(in_array($_POST['DecretoCaracterizacion']['decreto_id'] , $model))
$this->redirect(array('view', 'id'=>$id, 'error'=>$error));
elseif($dec->save())
$this->redirect(array('view', 'id'=>$id));
}
$this->render('view',array(
'model'=>$this->loadModel($id), 'dec'=>$dec
));
}
表示DecretoCaracterizacion的渲染空间
<?php
/* @var $this EquipoController */
/* @var $car Equipo */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'decretocaracterizacion-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,
)); ?>
<p class="note">Campos con <span class="required">*</span> son requeridos.</p>
<?php echo $form->errorSummary($dec); ?>
<div class="row">
<?php echo $form->labelEx($dec,'decreto_id'); ?>
<?php echo $form->dropDownList($dec,'decreto_id',CHtml::listData(Decreto::model()->findAll(),'id','ndecreto'), array('empty'=>'Seleccione decreto', 'value'=>'0')); ?> <p class="note">Valor por defecto "0"</p>
<?php echo $form->error($dec,'decreto_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($dec,'caracterizacion_id'); ?>
<?php echo $form->textField($dec, 'caracterizacion_id', array('readOnly'=>'true', 'value'=>$car->id)); ?>
<?php echo $form->error($dec,'caracterizacion_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($dec->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
我想要的是什么,我不希望复制的是decreto_id,即使我看到它没有被复制,我在DB中看到它被复制但不知道为什么它没有在视图中显示。
更新:这是一个可以有很多破坏的特征,但在1个特征中不能有相同的破坏。
模型
<?php
/*$decre = array();
foreach($model->decreto_caracterizacion as $val) {
$decre[] = $val->decreto_id;
}
/**
* This is the model class for table "caracterizacion".
*
* The followings are the available columns in table 'caracterizacion':
* @property string $id
* @property string $parametro
* @property integer $decreto_id
*/
class Caracterizacion extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'caracterizacion';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('parametro', 'required'),
array('decreto_id', 'length', 'max'=>256),
array('parametro', 'length', 'max'=>256),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, parametro', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'decreto'=>array(self::MANY_MANY,'decreto','decreto_caracterizacion(caracterizacion_id,decreto_id)'),
'peticion'=>array(self::BELONGS_TO,'Peticion','peticion_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'parametro' => 'Parametro',
'decreto_id' => 'Decreto',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('parametro',$this->parametro,true);
$criteria->compare('decreto_id',$this->decreto_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Caracterizacion the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function columna($variable)
{
$sql="alter table parametro add ".$variable." boolean";
$data=Yii::app()->db->createCommand($sql)->execute();
return $data;
}
}
答案 0 :(得分:0)
您可以随时查看任何您认为错误的内容false
,并在保存路由的任何步骤中停止该过程!
我更愿意使用beforeValidate()
并检查现有记录,其中一些列完全相同,
您也可以自定义validation rules
示例:
public function beforeValidare()
{
$criteria = new CDbCriteria();
$criteria->addCondition('....'); // add conditions to meet your requirements
if(MyModel::model()->exists($criteria)) // check if records with those conditions exists
return false; // do this to break the chain
return parent::beforeValidate(); // don't forget this line
}