Rabu, 06 September 2017

Validators 2 model

<?php

/**
 * This is the model class for table "jenis".
 *
 * The followings are the available columns in table 'jenis':
 * @property integer $id
 * @property integer $jenis
 * @property integer $tahun
 * @property string $fullname
 * @property string $file
 * @property string $status
 *
 * The followings are the available model relations:
 * @property ProgramRancangan[] $programRancangans
 * @property Rancangan[] $rancangans
 */
class Jenis extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public $oldtahun;
    public $oldjenis;

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

/**
* @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('jenis, tahun, fullname, status', 'required'),
array('tahun', 'checkUniqueness'),

// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, jenis, tahun, fullname, file, status', 'safe', 'on'=>'search'),
);
}

// @todo Please modify the following code to remove attributes that should not be

public function checkUniqueness($attribute,$params)
    {
        if($this->tahun !== $this->oldtahun || $this->jenis !== $this->oldjenis)
        {
            $model = jenis::model()->find('tahun = ? AND jenis = ?', array($this->tahun, $this->jenis));
            if($model != null)
                $this->addError('tahun','tahun sudah ada');
        }  
    }

    protected function afterFind()
    {
        parent::afterFind();
        $this->oldtahun = $this->tahun;
        $this->oldjenis = $this->jenis;

    }
}

0 komentar:

Posting Komentar