zoukankan      html  css  js  c++  java
  • tp3.2 curd学习1

    tp3.2

    1、批量新增或者更新,addAll()方法。

    先上测试代码:

     public function t1()
        {
            $data = [
                [
                    'id' => 3,
                    'name' => 'aaaa',
                    'age' => 14
                ],  [
                    'id' => 4,
                    'age' => 13,
                    'name' => 'bbb',
                ],[
                    'id'   => 1,
                    'name' => 'ccccc666',
                    'age'  => 15,
                ],[
                    'id' => 5,
                    'name' => 'zzzz',
                    'age' => 22
                ]
            ];
            $user = D('User');
            $user->addAll($data,[], true);
            print_r($user->getLastSql());
        }

    测试结果:

    1)原来数据库中已经存有一条数据

    在执行t1方法后,数据如下,

     2)打印出来 执行的sql语句是:

    REPLACE INTO `user` (`id`,`name`,`age`) VALUES ('3','aaaa','14'),('4','13','bbb'),('1','ccccc666','15'),('5','zzzz','22')

    综上,

    根据唯一条件,去更新或新增记录。

    ps

    注意二维数组data里的,一维数组,字段个数 和顺序要一样,不然值会错位,或者报错(字段数不一样)。

     2、附上 tp3.2框架中的addAll方法,与db的insertAll方法。

     public function addAll($dataList,$options=array(),$replace=false){
            if(empty($dataList)) {
                $this->error = L('_DATA_TYPE_INVALID_');
                return false;
            }
            // 数据处理
            foreach ($dataList as $key=>$data){
                $dataList[$key] = $this->_facade($data);
            }
            // 分析表达式
            $options =  $this->_parseOptions($options);
            // 写入数据到数据库
            $result = $this->db->insertAll($dataList,$options,$replace);
            if(false !== $result ) {
                $insertId   =   $this->getLastInsID();
                if($insertId) {
                    return $insertId;
                }
            }
            return $result;
        }
    /**
         * 批量插入记录
         * @access public
         * @param mixed $dataSet 数据集
         * @param array $options 参数表达式
         * @param boolean $replace 是否replace
         * @return false | integer
         */
        public function insertAll($dataSet,$options=array(),$replace=false) {
            $values  =  array();
            $this->model  =   $options['model'];
            if(!is_array($dataSet[0])) return false;
            $this->parseBind(!empty($options['bind'])?$options['bind']:array());
            $fields =   array_map(array($this,'parseKey'),array_keys($dataSet[0]));
            foreach ($dataSet as $data){
                $value   =  array();
                foreach ($data as $key=>$val){
                    if(is_array($val) && 'exp' == $val[0]){
                        $value[]   =  $val[1];
                    }elseif(is_null($val)){
                        $value[]   =   'NULL';
                    }elseif(is_scalar($val)){
                        if(0===strpos($val,':') && in_array($val,array_keys($this->bind))){
                            $value[]   =   $this->parseValue($val);
                        }else{
                            $name       =   count($this->bind);
                            $value[]   =   ':'.$name;
                            $this->bindParam($name,$val);
                        }
                    }
                }
                $values[]    = '('.implode(',', $value).')';
            }
            // 兼容数字传入方式
            $replace= (is_numeric($replace) && $replace>0)?true:$replace;
            $sql    =  (true===$replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES '.implode(',',$values).$this->parseDuplicate($replace);
            $sql    .= $this->parseComment(!empty($options['comment'])?$options['comment']:'');
            return $this->execute($sql,!empty($options['fetch_sql']) ? true : false);
        }
  • 相关阅读:
    POJ 3258 二分答案
    Prototype 模式示例代码 (C++)
    offsetof 和 container_of
    二进制整数中的“1”
    Binary Tree Traversal Algorithms (二叉树遍历算法)
    A* Pathfinding Algorithm
    Axis­ Aligned 
Rectangles (Google 面试题, 2016网易游戏校招笔试)
    [LeetCode] Burst Ballons
    C++ 继承语义下构造函数中的对象指针
    kill-9和kill-15的区别
  • 原文地址:https://www.cnblogs.com/linst/p/10237782.html
Copyright © 2011-2022 走看看