public function create()
    {
        $autoIncrementField = $this->getAutoIncrementField();
        if ($autoIncrementField && $this->$autoIncrementField === null) {
            $this->$autoIncrementField = $this->getNextAutoIncrementId();
        }

        $fields = $this->getFields();
        foreach ($this->getAutoFilledData(self::OP_CREATE) as $field => $value) {
            /** @noinspection NotOptimalIfConditionsInspection */
            if (!in_array($field, $fields, true) || $this->$field !== null) {
                continue;
            }
            $this->$field = $value;
        }

        $this->validate($fields);

        $this->eventsManager->fireEvent('model:beforeSave', $this);
        $this->eventsManager->fireEvent('model:beforeCreate', $this);

        $fieldValues = [];
        $defaultValueFields = [];
        foreach ($fields as $field) {
            if ($this->$field !== null) {
                $fieldValues[$field] = $this->$field;
            } elseif ($field !== $autoIncrementField) {
                $defaultValueFields[] = $field;
            }
        }

        foreach ($this->getJsonFields() as $field) {
            if (is_array($this->$field)) {
                $fieldValues[$field] = json_encode($this->$field, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            }
        }

        /**
         * @var \ManaPHP\DbInterface $connection
         */
        $connection = $this->_di->getShared($this->getDb($this));

        if ($autoIncrementField && $this->$autoIncrementField === null) {
            $this->$autoIncrementField = (int)$connection->insert($this->getSource($this), $fieldValues, true);
        } else {
            $connection->insert($this->getSource($this), $fieldValues);
        }

        if ($defaultValueFields) {
            if ($r = static::query(null, $this)->select($defaultValueFields)->where($this->_getPrimaryKeyValuePairs())->fetch(true)) {
                foreach ($r[0] as $field => $value) {
                    $this->$field = $value;
                }
            }
        }

        $this->_snapshot = $this->toArray();

        $this->eventsManager->fireEvent('model:afterCreate', $this);
        $this->eventsManager->fireEvent('model:afterSave', $this);

        return $this;
    }
public function create()
    {
        $autoIncrementField = $this->getAutoIncrementField();
        if ($autoIncrementField && $this->$autoIncrementField === null) {
            $this->$autoIncrementField = $this->getNextAutoIncrementId();
        }

        $fields = $this->getFields();
        foreach ($this->getAutoFilledData(self::OP_CREATE) as $field => $value) {
            /** @noinspection NotOptimalIfConditionsInspection */
            if (!in_array($field, $fields, true) || $this->$field !== null) {
                continue;
            }
            $this->$field = $value;
        }

        $this->validate($fields);

        $this->eventsManager->fireEvent('model:beforeSave', $this);
        $this->eventsManager->fireEvent('model:beforeCreate', $this);

        $fieldValues = [];
        $defaultValueFields = [];
        foreach ($fields as $field) {
            if ($this->$field !== null) {
                $fieldValues[$field] = $this->$field;
            } elseif ($field !== $autoIncrementField) {
                $defaultValueFields[] = $field;
            }
        }

        foreach ($this->getJsonFields() as $field) {
            if (is_array($this->$field)) {
                $fieldValues[$field] = json_encode($this->$field, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            }
        }

        /**
         * @var \ManaPHP\DbInterface $connection
         */
        $connection = $this->_di->getShared($this->getDb($this));

        if ($autoIncrementField && $this->$autoIncrementField === null) {
            $this->$autoIncrementField = (int)$connection->insert($this->getSource($this), $fieldValues, true);
        } else {
            $connection->insert($this->getSource($this), $fieldValues);
        }

        if ($defaultValueFields) {
            if ($r = static::query(null, $this)->select($defaultValueFields)->where($this->_getPrimaryKeyValuePairs())->fetch(true)) {
                foreach ($r[0] as $field => $value) {
                    $this->$field = $value;
                }
            }
        }

        $this->_snapshot = $this->toArray();

        $this->eventsManager->fireEvent('model:afterCreate', $this);
        $this->eventsManager->fireEvent('model:afterSave', $this);

        return $this;
    }
public function create ( ) { $ autoIncrementField = $ this -> getAutoIncrementField ( ) ; if ( $ autoIncrementField && $ this -> $ autoIncrementField === null ) { $ this -> $ autoIncrementField = $ this -> getNextAutoIncrementId ( ) ; } $ fields = $ this -> getFields ( ) ; foreach ( $ this -> getAutoFilledData ( self :: OP_CREATE ) as $ field => $ value ) { /** @noinspection NotOptimalIfConditionsInspection */ if ( ! in_array ( $ field , $ fields , true ) || $ this -> $ field !== null ) { continue ; } $ this -> $ field = $ value ; } $ this -> validate ( $ fields ) ; $ this -> eventsManager -> fireEvent ( 'model:beforeSave' , $ this ) ; $ this -> eventsManager -> fireEvent ( 'model:beforeCreate' , $ this ) ; $ fieldValues = [ ] ; $ defaultValueFields = [ ] ; foreach ( $ fields as $ field ) { if ( $ this -> $ field !== null ) { $ fieldValues [ $ field ] = $ this -> $ field ; } elseif ( $ field !== $ autoIncrementField ) { $ defaultValueFields [ ] = $ field ; } } foreach ( $ this -> getJsonFields ( ) as $ field ) { if ( is_array ( $ this -> $ field ) ) { $ fieldValues [ $ field ] = json_encode ( $ this -> $ field , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ; } } /**
         * @var \ManaPHP\DbInterface $connection
         */ $ connection = $ this -> _di -> getShared ( $ this -> getDb ( $ this ) ) ; if ( $ autoIncrementField && $ this -> $ autoIncrementField === null ) { $ this -> $ autoIncrementField = ( int ) $ connection -> insert ( $ this -> getSource ( $ this ) , $ fieldValues , true ) ; } else { $ connection -> insert ( $ this -> getSource ( $ this ) , $ fieldValues ) ; } if ( $ defaultValueFields ) { if ( $ r = static :: query ( null , $ this ) -> select ( $ defaultValueFields ) -> where ( $ this -> _getPrimaryKeyValuePairs ( ) ) -> fetch ( true ) ) { foreach ( $ r [ 0 ] as $ field => $ value ) { $ this -> $ field = $ value ; } } } $ this -> _snapshot = $ this -> toArray ( ) ; $ this -> eventsManager -> fireEvent ( 'model:afterCreate' , $ this ) ; $ this -> eventsManager -> fireEvent ( 'model:afterSave' , $ this ) ; return $ this ; }
Inserts a model instance. If the instance already exists in the persistence it will throw an exception

@return static
Inserts a model instance . If the instance already exists in the persistence it will throw an exception