<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model;

/**
 * 文档基础模型
 */
class SubsiteModel extends Model{
    //批量添加
    protected $dbcount=0;
    protected $syncKey="id";
    protected $syncData;
    protected $syncWhere=false;

    public function __construct($name = '', $tablePrefix = '', $connection = '')
    {
        parent::__construct($name, $tablePrefix, $connection);

        if(in_array($this->trueTableName,C('SUBSITE_TABLE'))){
            $SUBSITE_DB = C("SUBSITE_DB_CONFIG");
            $i = 1;
            foreach ($SUBSITE_DB as $k => $v) {
            $this->db($i,$v);
            $i++;
            }
            $this->dbcount = $i;
        }
    }
    //修改主键,默认id
    public function syncKey($key){
        $this->syncKey = $key;
        return $this;
    }
    //持续化数据条件
    protected function syncCreate($data)
    {
        $this->syncData = $data;
        if(isset($this->options['where'])){
            $this->syncWhere = $this->options['where'];
        }
    }
    //同步添加
    public function syncAdd($data)
    {
        $this->syncCreate($data);
        $syncVal = $this->db(0)->add();
        if(empty($syncVal)){
            return $syncVal;
        }
        $this->syncData[$this->syncKey] = $syncVal;
        for ($i=1; $i < $this->dbcount; $i++) { 
            $this->db($i)->add($this->syncData);
        }
        return $syncVal;
    }
    //同步修改
    public function syncSave($data)
    {
        $this->syncCreate($data);
        for ($i=0; $i < $this->dbcount; $i++) { 
           $this->db($i)->where($this->syncWhere)->save($this->syncData);
        }
        return true;
    }




}