DB\Jig\Mapper::insert PHP 메소드

insert() 공개 메소드

Insert new record
public insert ( ) : array
리턴 array
    function insert()
    {
        if ($this->id) {
            return $this->update();
        }
        $db = $this->db;
        $now = microtime(TRUE);
        while (($id = uniqid(NULL, TRUE)) && ($data =& $db->read($this->file)) && isset($data[$id]) && !connection_aborted()) {
            usleep(mt_rand(0, 100));
        }
        $this->id = $id;
        $pkey = ['_id' => $this->id];
        if (isset($this->trigger['beforeinsert']) && \Base::instance()->call($this->trigger['beforeinsert'], [$this, $pkey]) === FALSE) {
            return $this->document;
        }
        $data[$id] = $this->document;
        $db->write($this->file, $data);
        $db->jot('(' . sprintf('%.1f', 1000.0 * (microtime(TRUE) - $now)) . 'ms) ' . $this->file . ' [insert] ' . json_encode($this->document));
        if (isset($this->trigger['afterinsert'])) {
            \Base::instance()->call($this->trigger['afterinsert'], [$this, $pkey]);
        }
        $this->load(['@_id=?', $this->id]);
        return $this->document;
    }