<?php
namespace admin\model;
use source\Model;
/**
 * 剧本稿件模型
 */
class Goods extends Model{
	
	/* 表名 */
	protected $tableName = 'goods';
	


	
    /* 自动验证规则 */
    protected $_validate = array(
       /* array('name', '/^[a-zA-Z]\w{0,30}$/', '文档标识不合法', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        array('name', '', '标识已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
        array('title', 'require', '标题不能为空', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        array('category_id', 'require', '分类不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
        array('category_id', 'require', '分类不能为空', self::EXISTS_VALIDATE , 'regex', self::MODEL_UPDATE),
        array('category_id,type', 'check_category', '内容类型不正确', self::MUST_VALIDATE , 'function', self::MODEL_INSERT),
        array('category_id', 'check_category', '该分类不允许发布内容', self::EXISTS_VALIDATE , 'function', self::MODEL_BOTH),
        array('model_id,category_id,pid', 'check_category_model', '该分类没有绑定当前模型', self::MUST_VALIDATE , 'function', self::MODEL_INSERT),*/
    );

    /* 自动完成规则 */
    protected $_auto = array(
        /*array('uid', 'session', self::MODEL_INSERT, 'function', 'user_auth.uid'),
        array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('description', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('root', 'getRoot', self::MODEL_BOTH, 'callback'),
        array('attach', 0, self::MODEL_INSERT),
        array('view', 0, self::MODEL_INSERT),
        array('comment', 0, self::MODEL_INSERT),
        array('extend', 0, self::MODEL_INSERT),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        //array('reply_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', 'getStatus', self::MODEL_BOTH, 'callback'),*/
    );
	
	
	function _after_insert($data){
		$id=$data['id'];
		$data=I('goods_attrvalue');
    	$attr_id=$data['attr_id'];
		$datalist=array();
		foreach ($attr_id as $k => $v) {
			if(empty($data['value'][$k])){
			    continue;
			}
			$datalist[]=array(
				'goods_id'=>$id,
				'attr_id'=>$v,
				'value'=>$data['value'][$k],
				'price'=>$data['price'][$k],
			);
		}
		D('Home/GoodsAttrvalue')->addAll($datalist);
	}
	
	function _after_update($data){
		$id=$data['id'];
		$data=I('goods_attrvalue');
    	$attr_id=$data['attr_id'];
		$datalist=array();
		foreach ($attr_id as $k => $v) {
			if(empty($data['value'][$k])){
			    continue;
			}
			$datalist[]=array(
				'goods_id'=>$id,
				'attr_id'=>$v,
				'value'=>$data['value'][$k],
				'price'=>$data['price'][$k],
			);
		}
		D('Home/GoodsAttrvalue')->addAll($datalist);
	}
	


}
