<?php

/**
 * This is the model class for table "business".
 *
 * The followings are the available columns in table 'business':
 * @property integer $id
 * @property string $account
 * @property string $name
 * @property string $password
 * @property string $desc
 * @property string $mobile_phone
 * @property string $email
 * @property string $company_name
 * @property string $company_address
 * @property string $contacter
 * @property string $zipcode
 * @property string $logo
 * @property string $add_time
 * @property integer $industry_id
 * @property string $wechat_account
 * @property string $wechat_appid
 * @property string $wechat_appsecurity
 *
 * The followings are the available model relations:
 * @property */
class Business extends Model
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Business the static model class
	*/
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	*/
	public function tableName()
	{
		return '{{business}}';
	}

	/**
	 * @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('account, name, desc, mobile_phone, company_address, contacter, add_time, wechat_account', 'required'),
			array('industry_id', 'numerical', 'integerOnly'=>true),
			array('account, wechat_account, wechat_appid, wechat_appsecurity', 'length', 'max'=>30),
			array('name, company_name, logo', 'length', 'max'=>120),
			array('password', 'length', 'max'=>50),
			array('mobile_phone', 'length', 'max'=>11),
			array('email, contacter', 'length', 'max'=>20),
			array('company_address', 'length', 'max'=>200),
			array('zipcode', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, account, name, password, desc, mobile_phone, email, company_name, company_address, contacter, zipcode, logo, add_time, industry_id, wechat_account, wechat_appid, wechat_appsecurity', '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(
			'industry' => array(self::BELONGS_TO,'Industry','industry_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	*/
	public function attributeLabels()
	{
		return array(
			'id' => '商家ID',
			'account' => '商户帐号',
			'name' => '商家名称',
			'password' => '登录密码',
			'desc' => '描述',
			'mobile_phone' => '手机号',
			'email' => '邮箱',
			'company_name' => '公司名称',
			'company_address' => '公司地址',
			'contacter' => '联系人',
			'zipcode' => '邮编',
			'logo' => '商标',
			'add_time' => '添加时间',
			'industry_id' => '行业',
			'wechat_account' => '微信帐号',
			'wechat_appid' => '微信开发者帐号',
			'wechat_appsecurity' => '微信开发帐号密码',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	*/
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		//$criteria->order = 'id DESC';

		$criteria->compare('account',$this->account,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('mobile_phone',$this->mobile_phone,true);
		$criteria->compare('contacter',$this->contacter,true);
		if(!empty($this->industry_id))$criteria->addInCondition('industry_id' ,Industry::model()->getAllChildrenIds($this->industry_id));
		$criteria->compare('wechat_account',$this->wechat_account,true);
		$criteria->with=array(
			'industry',
		);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}