Contao\DC_Table::create PHP Method

create() public method

Insert a new row into a database table
public create ( array $set = [] )
$set array
    public function create($set = array())
    {
        if ($GLOBALS['TL_DCA'][$this->strTable]['config']['notCreatable']) {
            throw new InternalServerErrorException('Table "' . $this->strTable . '" is not creatable.');
        }
        // Get all default values for the new entry
        foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $k => $v) {
            // Use array_key_exists here (see #5252)
            if (array_key_exists('default', $v)) {
                $this->set[$k] = is_array($v['default']) ? serialize($v['default']) : $v['default'];
                // Encrypt the default value (see #3740)
                if ($GLOBALS['TL_DCA'][$this->strTable]['fields'][$k]['eval']['encrypt']) {
                    $this->set[$k] = \Encryption::encrypt($this->set[$k]);
                }
            }
        }
        // Set passed values
        if (!empty($set) && is_array($set)) {
            $this->set = array_merge($this->set, $set);
        }
        // Get the new position
        $this->getNewPosition('new', strlen(\Input::get('pid')) ? \Input::get('pid') : null, \Input::get('mode') == '2' ? true : false);
        // Dynamically set the parent table of tl_content
        if ($GLOBALS['TL_DCA'][$this->strTable]['config']['dynamicPtable']) {
            $this->set['ptable'] = $this->ptable;
        }
        /** @var SessionInterface $objSession */
        $objSession = \System::getContainer()->get('session');
        // Empty the clipboard
        $arrClipboard = $objSession->get('CLIPBOARD');
        $arrClipboard[$this->strTable] = array();
        $objSession->set('CLIPBOARD', $arrClipboard);
        // Insert the record if the table is not closed and switch to edit mode
        if (!$GLOBALS['TL_DCA'][$this->strTable]['config']['closed']) {
            $this->set['tstamp'] = 0;
            $objInsertStmt = $this->Database->prepare("INSERT INTO " . $this->strTable . " %s")->set($this->set)->execute();
            if ($objInsertStmt->affectedRows) {
                $s2e = $GLOBALS['TL_DCA'][$this->strTable]['config']['switchToEdit'] ? '&s2e=1' : '';
                $insertID = $objInsertStmt->insertId;
                /** @var AttributeBagInterface $objSessionBag */
                $objSessionBag = $objSession->getBag('contao_backend');
                // Save new record in the session
                $new_records = $objSessionBag->get('new_records');
                $new_records[$this->strTable][] = $insertID;
                $objSessionBag->set('new_records', $new_records);
                // Call the oncreate_callback
                if (is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['oncreate_callback'])) {
                    foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['oncreate_callback'] as $callback) {
                        if (is_array($callback)) {
                            $this->import($callback[0]);
                            $this->{$callback[0]}->{$callback[1]}($this->strTable, $insertID, $this->set, $this);
                        } elseif (is_callable($callback)) {
                            $callback($this->strTable, $insertID, $this->set, $this);
                        }
                    }
                }
                // Add a log entry
                $this->log('A new entry "' . $this->strTable . '.id=' . $insertID . '" has been created' . $this->getParentEntries($this->strTable, $insertID), __METHOD__, TL_GENERAL);
                $this->redirect($this->switchToEdit($insertID) . $s2e);
            }
        }
        $this->redirect($this->getReferer());
    }