<?php

/**
 * 学区
 */

/**
 * 学区
 *
 * @author Administrator
 */
class School {
    /**
     *
     * @var TINY_ADODB 主数据库
     */
    private $db;
    /**
     *
     * @var TINY_ADODB 从数据库
     */
    private $dbback;
    /**
     *
     * @var Memcache 缓存
     */
    private $mc;
    /**
     *
     * @var string 表名
     */
    private $table;
    /**
     *
     * @var 表中字段数组 
     */
    private $fields;
    
    /**
     * 初始化类
     * @param string $table 表名，默认为growth_bid_periods
     */
    public function __construct($table = 'school') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = $table;      
        $sqlfield = "DESC $this->table";
        $fieldsMKey = $sqlfield;
       // $this->mc->delete($fieldsMKey);
        $this->fields = $this->mc->get($fieldsMKey);
        if($this->fields === false) {
            $fields = $this->dbback->getAll($sqlfield);         
            foreach ($fields as $field) {
                $this->fields[] = $field['Field'];
            }
            $this->mc->set($fieldsMKey, $this->fields, 3600);
        }
    }

    /**
     * 
     * @param type $schoolId
     * @return type
     */
    public function findOneBy($schoolId) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,"id='$schoolId'");
        return $this->dbback->GetRow($sql);
    }
    

    
    /**
     * 
     * @param type $blockname
     * @return type
     */
    public function findAllIdByLikeName($schoolname) {
        $sql = SqlCombinHelper::select_sql($this->table, 'id',"schoolname like '%{$schoolname}%'");
        $arrSchoolIdOne = $this->mc->get($sql);
        if (!$arrSchoolIdOne) {
            $arrSchoolId = $this->dbback->getAll($sql);
            $arrSchoolIdOne = array();
            if (!CommonBase::isFullArray($arrSchoolId)) {return $arrSchoolIdOne;}
            foreach($arrBlockId as $k => $v) {
                $arrSchoolIdOne[] = $v['id'];
            }
            $this->mc->set($sql, $arrSchoolIdOne, 86400);
        }
        return $arrSchoolIdOne;
    }
    
    public function findAllBy($conf) {
        $sql = SqlCombinHelper::select_sql($this->table,$this->fields,$conf);
        return $this->dbback->getAll($sql);
    }
    
    
}

?>
