2013-12-12 46 views
0

我希望避免在數據庫上存儲的字段與數據庫上的數據相同(以避免重複),在yii中,我使用表單由gii創建,即時通訊在另一個視圖上製作渲染部分,因此您可能會在視圖動作中看到代碼,這是完全正常的,這裏是代碼。yii,如何避免提交按鈕形式的儲蓄

控制器/動作(圖)

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

形式renderpatial爲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被複制,即使我看到它沒有被複制,我看到數據庫被重複,但不知道爲什麼它沒有顯示在視圖中。

更新:theres one caracterizacion,可以有許多法定資產,但不能在1 caracterizacion具有相同的法定金。

模型

<?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; 
    } 
} 
+1

有你嘗試在 –

回答

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 
} 
+0

模型獨特的驗證規則,你會給我發送或輸入一個例子嗎?..在這個新的 – nosthertus

+0

我增加了一個例子 – tinybyte

+0

我想我的情況將decreto_ id,caracterizacion_id ?.請記住,1 caracterizacion可以有許多減少,也可以有另一個caracterizacion可以有幾乎相同的減少。如果是這樣,它會是(decreto_id,caracterizacion_id)對不對? – nosthertus