<?php

/**
 * This is the model class for table "{{customer_documents}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property string  $document_title
 * @property string  $customer_document
 * @property string  $isActive
 * @property integer $created_by
 * @property string  $created_date_time
 * @property integer $updated_by
 * @property string  $updated_date_time
 */
class CustomerDocuments extends CActiveRecord
{
    public function tableName()
    {
        return '{{customer_documents}}';
    }

    /**
     * Call this once to create the table:
     * CustomerDocuments::model()->createTable();
     */
    public function createTable()
    {
        $db  = Yii::app()->db;
        $sql = "CREATE TABLE IF NOT EXISTS `customer_documents` (
            `id`                INT(11)      NOT NULL AUTO_INCREMENT,
            `customer_id`       INT(11)      NOT NULL,
            `document_title`    VARCHAR(255) NOT NULL,
            `customer_document` VARCHAR(255) NOT NULL,
            `isActive`          CHAR(1)      NOT NULL DEFAULT 'Y',
            `created_by`        INT(11)      DEFAULT NULL,
            `created_date_time` DATETIME     DEFAULT NULL,
            `updated_by`        INT(11)      DEFAULT NULL,
            `updated_date_time` DATETIME     DEFAULT NULL,
            PRIMARY KEY (`id`),
            KEY `idx_customer_id` (`customer_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

        $db->createCommand($sql)->execute();
    }

    public function rules()
    {
        return array(
            array('customer_id, document_title, customer_document', 'required'),
            array('customer_id, created_by, updated_by', 'numerical', 'integerOnly' => true),
            array('isActive', 'length', 'max' => 1),
            array('id, customer_id, document_title, customer_document, isActive, created_by, created_date_time, updated_by, updated_date_time', 'safe', 'on' => 'search'),
        );
    }

    public function relations()
    {
        return array();
    }

    public function attributeLabels()
    {
        return array(
            'id'                 => 'ID',
            'customer_id'        => 'Customer',
            'document_title'     => 'Document Title',
            'customer_document'  => 'Document',
            'isActive'           => 'Is Active',
            'created_by'         => 'Created By',
            'created_date_time'  => 'Created Date Time',
            'updated_by'         => 'Updated By',
            'updated_date_time'  => 'Updated Date Time',
        );
    }

    public function beforeSave()
    {
        if ($this->isNewRecord) {
            $this->created_by        = Yii::app()->user->id;
            $this->created_date_time = date('Y-m-d H:i:s');
            $this->isActive          = 'Y';
        }
        $this->updated_by        = Yii::app()->user->id;
        $this->updated_date_time = date('Y-m-d H:i:s');
        return parent::beforeSave();
    }

    public function search()
    {
        $criteria = new CDbCriteria;
        $criteria->compare('id',                $this->id);
        $criteria->compare('customer_id',       $this->customer_id);
        $criteria->compare('document_title',    $this->document_title,   true);
        $criteria->compare('customer_document', $this->customer_document, true);
        $criteria->compare('isActive',          $this->isActive,         true);
        $criteria->compare('created_by',        $this->created_by);
        $criteria->compare('created_date_time', $this->created_date_time, true);
        $criteria->compare('updated_by',        $this->updated_by);
        $criteria->compare('updated_date_time', $this->updated_date_time, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }
}