public function save()
    {
        $builder = static::query();

        /**
         * Get the current primary key value
         */
        $primary_key_value = $this->getKeyValue();

        /**
         * If primary key value is null, we are going to start the creation of new
         * row
         */
        if ($primary_key_value == null) {
            /**
             * Insert information in the database
             */
            $r = $builder->insert($this->attributes);

            /**
             * We get a last insertion id value
             */
            $primary_key_value = $builder->getLastInsertId();

            /**
             * Transtype value to the define primary key type
             */
            if ($this->primary_key_type == 'int') {
                $primary_key_value = (int) $primary_key_value;
            } elseif ($this->primary_key_type == 'float') {
                $primary_key_value = (float) $primary_key_value;
            } elseif ($this->primary_key_type == 'double') {
                $primary_key_value = (double) $primary_key_value;
            } else {
                $primary_key_value = (string) $primary_key_value;
            }

            /**
             * Set the primary key value
             */
            $this->attributes[$this->primary_key] = $primary_key_value;

            $this->original = $this->attributes;

            if ($r == 1) {
                $this->fireEvent('oncreated');
            }

            return $r;
        }

        if (!$builder->exists($this->primary_key, $primary_key_value)) {
            return 0;
        }

        $this->original[$this->primary_key] = $primary_key_value;

        $update_data = [];

        foreach ($this->attributes as $key => $value) {
            if (!isset($this->original[$key])
                || $this->original[$key] != $value) {
                $update_data[$key] = $value;
            }
        }

        $r = $builder
            ->where($this->primary_key, $primary_key_value)
            ->update($update_data);

        if ($r == 1) {
            $this->fireEvent('onupdate');
        }

        return $r;
    }