Note that there are some explanatory texts on larger screens.

plurals
  1. POyii, how to avoid savings in submitbutton form
    text
    copied!<p>i want to avoid the saving on database whenever the data thats being stored on the field is the same as is being on db (to avoid duplications), in yii, i use the form made by gii, im making a renderpartial on another view, so you might see the code in a view action, its totally normal, here are the codes.</p> <p>controller/action(view)</p> <pre><code>public function actionView($id) { $dec=new DecretoCaracterizacion; if(isset($_POST['DecretoCaracterizacion'])) { $error='error'; $model=DecretoCaracterizacion::model()-&gt;findAll(); $dec-&gt;attributes=$_POST['DecretoCaracterizacion']; if(in_array($_POST['DecretoCaracterizacion']['decreto_id'] , $model)) $this-&gt;redirect(array('view', 'id'=&gt;$id, 'error'=&gt;$error)); elseif($dec-&gt;save()) $this-&gt;redirect(array('view', 'id'=&gt;$id)); } $this-&gt;render('view',array( 'model'=&gt;$this-&gt;loadModel($id), 'dec'=&gt;$dec )); } </code></pre> <p>form renderpatial for DecretoCaracterizacion</p> <pre><code>&lt;?php /* @var $this EquipoController */ /* @var $car Equipo */ /* @var $form CActiveForm */ ?&gt; &lt;div class="form"&gt; &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array( 'id'=&gt;'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'=&gt;false, )); ?&gt; &lt;p class="note"&gt;Campos con &lt;span class="required"&gt;*&lt;/span&gt; son requeridos.&lt;/p&gt; &lt;?php echo $form-&gt;errorSummary($dec); ?&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($dec,'decreto_id'); ?&gt; &lt;?php echo $form-&gt;dropDownList($dec,'decreto_id',CHtml::listData(Decreto::model()-&gt;findAll(),'id','ndecreto'), array('empty'=&gt;'Seleccione decreto', 'value'=&gt;'0')); ?&gt; &lt;p class="note"&gt;Valor por defecto "0"&lt;/p&gt; &lt;?php echo $form-&gt;error($dec,'decreto_id'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($dec,'caracterizacion_id'); ?&gt; &lt;?php echo $form-&gt;textField($dec, 'caracterizacion_id', array('readOnly'=&gt;'true', 'value'=&gt;$car-&gt;id)); ?&gt; &lt;?php echo $form-&gt;error($dec,'caracterizacion_id'); ?&gt; &lt;/div&gt; &lt;div class="row buttons"&gt; &lt;?php echo CHtml::submitButton($dec-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt; &lt;/div&gt; &lt;?php $this-&gt;endWidget(); ?&gt; &lt;/div&gt;&lt;!-- form --&gt; </code></pre> <p>what i want here, i dont want the decreto_id to be duplicated, even if i see its not being duplicated, i see in DB thats being duplicated but dont know why its not shown in the view.</p> <p>update: theres one caracterizacion that can have many decretos, but cant have same decretos in 1 caracterizacion.</p> <p>model</p> <pre><code>&lt;?php /*$decre = array(); foreach($model-&gt;decreto_caracterizacion as $val) { $decre[] = $val-&gt;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'=&gt;256), array('parametro', 'length', 'max'=&gt;256), // The following rule is used by search(). // @todo Please remove those attributes that should not be searched. array('id, parametro', 'safe', 'on'=&gt;'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'=&gt;array(self::MANY_MANY,'decreto','decreto_caracterizacion(caracterizacion_id,decreto_id)'), 'peticion'=&gt;array(self::BELONGS_TO,'Peticion','peticion_id'), ); } /** * @return array customized attribute labels (name=&gt;label) */ public function attributeLabels() { return array( 'id' =&gt; 'ID', 'parametro' =&gt; 'Parametro', 'decreto_id' =&gt; '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-&gt;compare('id',$this-&gt;id,true); $criteria-&gt;compare('parametro',$this-&gt;parametro,true); $criteria-&gt;compare('decreto_id',$this-&gt;decreto_id); return new CActiveDataProvider($this, array( 'criteria'=&gt;$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()-&gt;db-&gt;createCommand($sql)-&gt;execute(); return $data; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload