简化IN条件拼接.

@param string | array $data
@param string
@param boolean in or not in, default is in.

@farwish
545295
public static function joinCondition($data, $column, $in = true)
    {
        $join = is_array($data) ? (join(',', $data) ?: -1) : ($data ?: -1);
        $kw = $in ? "IN" : "NOT IN";
        return "$column $kw(" . $join . ")";
    }
1081906
public function addIndex(array $data)
    {
        if (!is_array($data)) {
            throw new \Exception('你的索引参数不是一个数组');
        }
        if (isset($data[0]) && is_array($data[0])) {
            // 多维数组
            foreach ($data as $v) {
                $this->getIndex()->add(new \XSDocument($v));
            }
        } else {
            // 一维数组
            $this->getIndex()->add(new \XSDocument($data));
        }
        //索引是否立即生效
        if ($this->flush_index) {
            $this->getIndex()->flushIndex();
        }
        return $this->getIndex();
    }
1054904
public function add(array $data, $cols = null) {
		if (is_array($cols)) {
			//筛选$data列
			foreach ($data as $k => $v) {
				if (!in_array($k, $cols, true)) {
					unset($data[$k]);
				}
			}
		}
		$query = $this->newInsert()->map($data);
		$result = $this->execute($query);
		if (!empty($this->_primary_key)) {
			return intval($result['_insert_id']);
		} else {
			return null;
		}
	}
1107457
private function init($data=null)
    {
        if (!is_null($this->stack)) return $this;
        
        if (!is_null($data))  $this->data = $data;
        if (!is_array($this->data) || empty($this->data)) return $this;
        
        // 先转为 id 为键的数据
        $t_data = array();
        $pids   = array();
        foreach ($this->data as $k => $v) {
            if (!isset($v[$this->fields[0]])) {
                throw new \InvalidArgumentException('tree fields error:' . implode(',', $this->fields));
                break; // 如果数组结构不符合，跳过
            }
            $t_data[$v[$this->fields[0]]] = $v;
            $pids[$v[$this->fields[1]]] = $v[$this->fields[1]];
        }
        $this->data = & $t_data;
        
        // 修正root，当parent_id=root不存在时取最小的parent_id作为root
        if (!isset($pids[$this->root])) {
            ksort($pids);
            $this->root = array_keys($pids)[0];
        }
        
        // 先生成父子级的二维的数组
        foreach ($this->data as $node) {
            $this->stack[$node[$this->fields[1]]][$node[$this->fields[0]]] = $node;
        }
        
        
        // 递归遍历生成 完整的带有 items 的数组
        foreach ($this->stack as $k => $v) {
            $this->handleStackElement($k);
        }
        //print_r($this->stack);exit;
    }
233030
public function set(array $data)
    {
        $this->data = [];
        $this->replace = false;

        foreach ($data as $key => $value) {
            $this->add($key, $value);
        }
    }
370239
protected function init($data)
    {
        $i = 0;

        foreach ($data as $header => $value) {
            $column = self::formatColumnName($i);
            $this->setHeader($column, $header);
            ++$i;
        }
        $this->setBoldLabels();
    }
990114
public function create($data='') {
        // 如果没有传值默认取POST数据
        if(empty($data)) {
            $data   =   I('post.');
        }elseif(is_object($data)){
            $data   =   get_object_vars($data);
        }
        // 验证数据
        if(empty($data) || !is_array($data)) {
            $this->error = L('_DATA_TYPE_INVALID_');
            return false;
        }

        // 检测提交字段的合法性
        if(isset($this->options['field'])) { // $this->field('field1,field2...')->create()
            $fields =   $this->options['field'];
            unset($this->options['field']);
        }
        if(isset($fields)) {
            if(is_string($fields)) {
                $fields =   explode(',',$fields);
            }
        }

        // 验证完成生成数据对象
        if($this->autoCheckFields) { // 开启字段检测 则过滤非法字段数据
            $fields =   $this->getDbFields();
            foreach ($data as $key=>$val){
                if(!in_array($key,$fields)) {
                    unset($data[$key]);
                }elseif(MAGIC_QUOTES_GPC && is_string($val)){
                    $data[$key] =   stripslashes($val);
                }
            }
        }

        // 赋值当前数据对象
        $this->data =   $data;
        // 返回创建的数据以供其他调用
        return $data;
     }
556262
public function create($data='',$type='') {
        // 如果没有传值默认取POST数据
        if(empty($data)) {
            $data   =   I('post.');
        }elseif(is_object($data)){
            $data   =   get_object_vars($data);
        }
        // 验证数据
        if(empty($data) || !is_array($data)) {
            $this->error = L('_DATA_TYPE_INVALID_');
            return false;
        }

        // 状态
        $type = $type?:(!empty($data[$this->getPk()])?self::MODEL_UPDATE:self::MODEL_INSERT);

        // 检查字段映射
		$data =	$this->parseFieldsMap($data,0);

        // 检测提交字段的合法性
        if(isset($this->options['field'])) { // $this->field('field1,field2...')->create()
            $fields =   $this->options['field'];
            unset($this->options['field']);
        }elseif($type == self::MODEL_INSERT && isset($this->insertFields)) {
            $fields =   $this->insertFields;
        }elseif($type == self::MODEL_UPDATE && isset($this->updateFields)) {
            $fields =   $this->updateFields;
        }
        if(isset($fields)) {
            if(is_string($fields)) {
                $fields =   explode(',',$fields);
            }
            // 判断令牌验证字段
            if(C('TOKEN_ON'))   $fields[] = C('TOKEN_NAME', null, '__hash__');
            foreach ($data as $key=>$val){
                if(!in_array($key,$fields)) {
                    unset($data[$key]);
                }
            }
        }

        // 数据自动验证
        if(!$this->autoValidation($data,$type)) return false;

        // 表单令牌验证
        if(!$this->autoCheckToken($data)) {
            $this->error = L('_TOKEN_ERROR_');
            return false;
        }

        // 验证完成生成数据对象
        if($this->autoCheckFields) { // 开启字段检测 则过滤非法字段数据
            $fields =   $this->getDbFields();
            foreach ($data as $key=>$val){
                if(!in_array($key,$fields)) {
                    unset($data[$key]);
                }elseif(MAGIC_QUOTES_GPC && is_string($val)){
                    $data[$key] =   stripslashes($val);
                }
            }
        }

        // 创建完成对数据进行自动处理
        $this->autoOperation($data,$type);
        // 赋值当前数据对象
        $this->data =   $data;
        // 返回创建的数据以供其他调用
        return $data;
     }
1147203
public function update($data) {
        #移除表中不存在字段
        $data = $this->connection->filterTableField($data);
        foreach ($data as $k => $v) {
            $this->build()->bindExpression('set', $k);
            $this->build()->bindParams('values', $v);
        }
        if (!$this->build()->getBindExpression('where')) {
            #有主键时使用主键做条件
            $pri = $this->connection->getPrimaryKey();
            if (isset($data[$pri])) {
                $this->where($pri, '=', $data[$pri]);
            }
        }
        return $this->connection->execute($this->build()->update(), $this->build()->getUpdateParams());
    }
993329
public function init(array $data = [])
    {
        foreach ($data as $key => $value) {
            $this->set($key, $value);
        }
    }
1325509
public function data($data, $value = null)
    {
        if (is_string($data)) {
            $this->data[$data] = $value;
        } else {
            // 清空数据
            $this->data = [];

            if (is_object($data)) {
                $data = get_object_vars($data);
            }

            if (true === $value) {
                // 数据对象赋值
                foreach ($data as $key => $value) {
                    $this->setAttr($key, $value, $data);
                }
            } else {
                $this->data = $data;
            }
        }

        return $this;
    }
798777
public function update(array $data)
    {
        $this->compileData($data);
        $arguments      = $this->arguments;
        $sql            = 'UPDATE '.$this->getFrom().' SET '.$this->setString;
        //设置了条件
        if ($this->condition->count() > 0) {
            $whereStr       = (string)$this->condition->compile();
            $whereArguments = $this->condition->arguments();
            $sql            = $sql.' where '.$whereStr;
            $arguments      = ArrayLib::array_add($arguments, $whereArguments);
        }
        return $this->connection->executeUpdate($sql, $arguments);
    }
928583
public function populate($data = array())
    {
        if (isset($data['name']) && $data['name'] !== null) {
            $this->name = $data['name'];
        }
        if (isset($data['locale']) && $data['locale'] !== null) {
            $this->locale = $data['locale'];
        }
    }
273164
public function set(array $data, array $options = []) {
		$defaults = ['init' => false];
		$options += $defaults;

		$cast = ($schema = $this->schema());

		foreach ($data as $key => $val) {
			unset($this->_increment[$key]);
			if (strpos($key, '.')) {
				$this->_setNested($key, $val);
				continue;
			}
			if ($cast) {
				$pathKey = $this->_pathKey;
				$model = $this->_model;
				$parent = $this;
				$val = $schema->cast($this, $key, $val, compact('pathKey', 'model', 'parent'));
			}
			if ($val instanceof self) {
				$val->_exists = $options['init'] && $this->_exists;
				$val->_pathKey = ($this->_pathKey ? "{$this->_pathKey}." : '') . $key;
				$val->_model = $val->_model ?: $this->_model;
				$val->_schema = $val->_schema ?: $this->_schema;
			}
			$this->_updated[$key] = $val;
		}
	}
1305189
function send($data)
    {
        $length = strlen($data);
        $written = 0;
        $t1 = microtime(true);
        //总超时，for循环中计时
        while ($written < $length)
        {
            $n = socket_send($this->sock, substr($data, $written), $length - $written, null);
            //超过总时间
            if (microtime(true) > $this->timeout_send + $t1)
            {
                return false;
            }
            if ($n === false)
            {
                $errno = socket_last_error($this->sock);
                //判断错误信息，EAGAIN EINTR，重写一次
                if ($errno == 11 or $errno == 4)
                {
                    continue;
                }
                else
                {
                    return false;
                }
            }
            $written += $n;
        }
        return $written;
    }
1361598
public static function set($id, array $data)
    {
        self::init();
        //无id调用临时id
        $id = self::getID($id);
        if ($id === null) {
            self::setTmpID(); //临时id也不存则新写临时id
            $id = self::getID($id);
        }

        if (isset(self::$data[$id])) {
            self::$data[$id] = array_merge(self::$data[$id], $data); //合并多个场景数据
        } else {
            self::$data[$id] = $data;
        }
        self::$modified[$id] = 1; //标记该id的场景部分修改

        return true;
    }
1167519
function push($data)
    {
        if(is_array($data) or is_object($data)) $data = serialize($data);
        //入队
        $this->data[] = $data;
        if($this->put_save) $this->save();
    }