src/Entity/Entity.php
public function &save()
    {
        // ---------------------------------------------------
        //  Populate defaults
        // ---------------------------------------------------

        if ($this->isNew()) {
            foreach ($this->default_field_values as $field_name => $field_value) {
                if (empty($this->values[$field_name]) && !array_key_exists($field_name, $this->values)) {
                    $this->setFieldValue($field_name, $field_value);
                }
            }
        }

        // ---------------------------------------------------
        //  Trigger on before save
        // ---------------------------------------------------

        $is_new = $this->isNew();
        $modifications = $this->getModifications();

        $this->triggerEvent('on_before_save', [$is_new, $modifications]);

        // ---------------------------------------------------
        //  Validate
        // ---------------------------------------------------

        $values_to_validate = $this->values;

        foreach ($this->fields as $field_name) {
            if (empty($values_to_validate[$field_name]) && !array_key_exists($field_name, $values_to_validate)) {
                $values_to_validate[$field_name] = null;
            }
        }

        $validator = new Validator($this->connection, $this->table_name, $this->getId(), $this->getOldFieldValue('id'), $values_to_validate);

        $this->validate($validator);

        if ($validator->hasErrors()) {
            throw $validator->createException();
        }

        // ---------------------------------------------------
        //  Do save
        // ---------------------------------------------------

        if ($this->isNew()) {
            $this->insert();
        } else {
            $this->update();
        }

        $this->triggerEvent('on_after_save', [$is_new, $modifications]);

        $this->pool->remember($this);

        return $this;
    }
public function &save()
    {
        // ---------------------------------------------------
        //  Populate defaults
        // ---------------------------------------------------

        if ($this->isNew()) {
            foreach ($this->default_field_values as $field_name => $field_value) {
                if (empty($this->values[$field_name]) && !array_key_exists($field_name, $this->values)) {
                    $this->setFieldValue($field_name, $field_value);
                }
            }
        }

        // ---------------------------------------------------
        //  Trigger on before save
        // ---------------------------------------------------

        $is_new = $this->isNew();
        $modifications = $this->getModifications();

        $this->triggerEvent('on_before_save', [$is_new, $modifications]);

        // ---------------------------------------------------
        //  Validate
        // ---------------------------------------------------

        $values_to_validate = $this->values;

        foreach ($this->fields as $field_name) {
            if (empty($values_to_validate[$field_name]) && !array_key_exists($field_name, $values_to_validate)) {
                $values_to_validate[$field_name] = null;
            }
        }

        $validator = new Validator($this->connection, $this->table_name, $this->getId(), $this->getOldFieldValue('id'), $values_to_validate);

        $this->validate($validator);

        if ($validator->hasErrors()) {
            throw $validator->createException();
        }

        // ---------------------------------------------------
        //  Do save
        // ---------------------------------------------------

        if ($this->isNew()) {
            $this->insert();
        } else {
            $this->update();
        }

        $this->triggerEvent('on_after_save', [$is_new, $modifications]);

        $this->pool->remember($this);

        return $this;
    }
Save object into database (insert or update).

@return $this
@throws ValidationException