Basecoat\DB::insert PHP Method

insert() public method

Insert record(s) in database. The last insert ID is stored in the $insertId class variable
public insert ( string $tablename, array &$data, string $modifiers = '', boolean $useMaster = true, string $action = 'INSERT' ) : integer
$tablename string name of table to insert into
$data array associative array of field/value pairs to insert, can be multi-dimensional for bulk inserts. All fields must be the same for each record for bulk inserts
$modifiers string modifier to use in query (i.e. IGNORE). Default is no modifier
$useMaster boolean set to TRUE to use the master server connection
$action string type of insert to perform (INSERT, REPLACE). Default is INSERT
return integer number of rows that were inserted
    public function insert($tablename, &$data, $modifiers = '', $useMaster = true, $action = 'INSERT')
    {
        if (!in_array($action, array('INSERT', 'REPLACE'))) {
            $action = 'INSERT';
        }
        $tablename = '`' . str_replace('.', '`.`', trim($tablename, '"\'`')) . '`';
        $query = $action . ' ' . $modifiers . ' INTO ' . $tablename;
        if ($useMaster) {
            $dbh =& self::$mdbh;
        } else {
            $dbh =& $this->dbh;
        }
        $insertCnt = 0;
        // Check for bulk insert
        if (is_array($data[key($data)])) {
            $recCount = count($data);
            // Get Field List
            $fields = array_keys($data[key($data)]);
            // Create field string
            $ifields = '`' . implode('`,`', $fields) . '`';
            $batches = ceil($recCount / $this->bulkInsertSize) . "\n";
            $unprepared = true;
            // Loop through inserts in batches
            for ($b = 1; $b <= $batches; $b++) {
                $bindings = array();
                $istart = ($b - 1) * $this->bulkInsertSize;
                $iend = min($this->bulkInsertSize, $recCount - $istart);
                $idata = array();
                $bindings = array();
                // Create bindings
                $data_block = array_slice($data, $istart, $this->bulkInsertSize);
                $i = 0;
                $bcnt = 0;
                foreach ($data_block as $rec) {
                    foreach ($rec as $field => $val) {
                        $idata[$field . $i] = $val;
                    }
                    $bindings[] = '(:' . implode($i . ',:', $fields) . $i . ')';
                    $bcnt += count($fields);
                    $i++;
                }
                // Check if a prepare statement needs to be run (first and last inserts)
                if ($unprepared || $b == $batches) {
                    $pquery = $query . ' (' . $ifields . ') VALUES ' . implode(',', $bindings);
                    $this->prepare($pquery, $useMaster);
                    $unprepared = false;
                }
                $iresult = $this->execute($idata, $useMaster);
                if ($iresult > 0) {
                    $insertCnt += $iresult;
                } else {
                    return $insertCnt;
                }
            }
            $this->insertId = $dbh->lastInsertId();
        } else {
            $fields = array_keys($data);
            $fieldCnt = count($data);
            $query .= ' (`' . implode('`,`', $fields) . '`) VALUES ( :' . implode(', :', $fields) . ' )';
            $this->prepare($query, $useMaster);
            if ($this->execute($data, $useMaster) == 1) {
                $insertCnt++;
                $this->insertId = $dbh->lastInsertId();
            }
        }
        return $insertCnt;
    }