Nette\Database\Table\Selection::insert PHP Метод

insert() публичный Метод

Inserts row in a table.
public insert ( $data ) : Nette\Database\Table\IRow | integer | boolean
Результат Nette\Database\Table\IRow | integer | boolean Returns IRow or number of affected rows for Selection or table without primary key
    public function insert($data)
    {
        if ($data instanceof self) {
            $return = $this->context->queryArgs($this->sqlBuilder->buildInsertQuery() . ' ' . $data->getSql(), $data->getSqlBuilder()->getParameters());
        } else {
            if ($data instanceof \Traversable) {
                $data = iterator_to_array($data);
            }
            $return = $this->context->query($this->sqlBuilder->buildInsertQuery() . ' ?values', $data);
        }
        $this->loadRefCache();
        if ($data instanceof self || $this->primary === NULL) {
            unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
            return $return->getRowCount();
        }
        $primaryKey = $this->context->getInsertId(($tmp = $this->getPrimarySequence()) ? implode('.', array_map([$this->context->getConnection()->getSupplementalDriver(), 'delimite'], explode('.', $tmp))) : NULL);
        if (!$primaryKey) {
            unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
            return $return->getRowCount();
        }
        if (is_array($this->getPrimary())) {
            $primaryKey = [];
            foreach ((array) $this->getPrimary() as $key) {
                if (!isset($data[$key])) {
                    return $data;
                }
                $primaryKey[$key] = $data[$key];
            }
            if (count($primaryKey) === 1) {
                $primaryKey = reset($primaryKey);
            }
        }
        $row = $this->createSelectionInstance()->select('*')->wherePrimary($primaryKey)->fetch();
        if ($this->rows !== NULL) {
            if ($signature = $row->getSignature(FALSE)) {
                $this->rows[$signature] = $row;
                $this->data[$signature] = $row;
            } else {
                $this->rows[] = $row;
                $this->data[] = $row;
            }
        }
        return $row;
    }

Usage Example

Пример #1
0
 /**
  * Default form handler
  */
 public function process()
 {
     /** @var ArrayHash $values */
     $values = $this->values;
     try {
         $this->onBeforeProcess($this, $values);
         if (isset($values->id)) {
             $this->onBeforeUpdate($this, $values);
             $arr = (array) $values;
             unset($arr['id']);
             $row = $this->selection->wherePrimary($values->id)->fetch();
             $row->update($arr);
             $this->onAfterUpdate($row, $this, $values);
         } else {
             $this->onBeforeInsert($this, $values);
             $row = $this->selection->insert($values);
             $this->onAfterInsert($row, $this, $values);
         }
         $this->onAfterProcess($row, $this, $values);
     } catch (\PDOException $e) {
         $this->addError($e->getMessage());
         dump($e);
         Debugger::log($e);
     }
 }
All Usage Examples Of Nette\Database\Table\Selection::insert