public function save($dataset, $condFields = null, $updateNulls = false)
    {
        // Handling conditions
        $condFields = $condFields ?: $this->getKeyName(true);

        $condFields = (array) $condFields;

        // Event
        $this->triggerEvent(
            'onBefore' . ucfirst(__FUNCTION__),
            [
                'dataset' => &$dataset,
                'condFields' => &$condFields,
                'updateNulls' => &$updateNulls,
            ]
        );

        $datasetClass = $this->datasetClass;

        $createDataset = new $datasetClass();
        $updateDataset = new $datasetClass();

        $fields = $this->getFields();
        
        foreach ($dataset as $k => $data) {
            if (!($data instanceof $this->dataClass)) {
                $data = $this->bindData($data);
            }

            $update = true;

            // If AI field is empty, use insert.
            foreach ($condFields as $field) {
                $extra = $fields[$field]->Extra ?? '';

                if ($extra === 'auto_increment' && !$data->$field) {
                    $update = false;

                    break;
                }
            }

            // Do save
            if ($update) {
                $updateDataset[] = $data;
            } else {
                $createDataset[] = $data;
            }

            $dataset[$k] = $data;
        }

        $this->create($createDataset);

        $this->update($updateDataset, $condFields, $updateNulls);

        // Event
        $this->triggerEvent(
            'onAfter' . ucfirst(__FUNCTION__),
            [
                'result' => &$dataset,
            ]
        );

        return $dataset;
    }
public function save($dataset, $condFields = null, $updateNulls = false)
    {
        // Handling conditions
        $condFields = $condFields ?: $this->getKeyName(true);

        $condFields = (array) $condFields;

        // Event
        $this->triggerEvent(
            'onBefore' . ucfirst(__FUNCTION__),
            [
                'dataset' => &$dataset,
                'condFields' => &$condFields,
                'updateNulls' => &$updateNulls,
            ]
        );

        $datasetClass = $this->datasetClass;

        $createDataset = new $datasetClass();
        $updateDataset = new $datasetClass();

        $fields = $this->getFields();
        
        foreach ($dataset as $k => $data) {
            if (!($data instanceof $this->dataClass)) {
                $data = $this->bindData($data);
            }

            $update = true;

            // If AI field is empty, use insert.
            foreach ($condFields as $field) {
                $extra = $fields[$field]->Extra ?? '';

                if ($extra === 'auto_increment' && !$data->$field) {
                    $update = false;

                    break;
                }
            }

            // Do save
            if ($update) {
                $updateDataset[] = $data;
            } else {
                $createDataset[] = $data;
            }

            $dataset[$k] = $data;
        }

        $this->create($createDataset);

        $this->update($updateDataset, $condFields, $updateNulls);

        // Event
        $this->triggerEvent(
            'onAfter' . ucfirst(__FUNCTION__),
            [
                'result' => &$dataset,
            ]
        );

        return $dataset;
    }
public function save ( $ dataset , $ condFields = null , $ updateNulls = false ) { // Handling conditions $ condFields = $ condFields ? : $ this -> getKeyName ( true ) ; $ condFields = ( array ) $ condFields ; // Event $ this -> triggerEvent ( 'onBefore' . ucfirst ( __FUNCTION__ ) , [ 'dataset' => & $ dataset , 'condFields' => & $ condFields , 'updateNulls' => & $ updateNulls , ] ) ; $ datasetClass = $ this -> datasetClass ; $ createDataset = new $ datasetClass ( ) ; $ updateDataset = new $ datasetClass ( ) ; $ fields = $ this -> getFields ( ) ; foreach ( $ dataset as $ k => $ data ) { if ( ! ( $ data instanceof $ this -> dataClass ) ) { $ data = $ this -> bindData ( $ data ) ; } $ update = true ; // If AI field is empty, use insert. foreach ( $ condFields as $ field ) { $ extra = $ fields [ $ field ] -> Extra ?? '' ; if ( $ extra === 'auto_increment' && ! $ data -> $ field ) { $ update = false ; break ; } } // Do save if ( $ update ) { $ updateDataset [ ] = $ data ; } else { $ createDataset [ ] = $ data ; } $ dataset [ $ k ] = $ data ; } $ this -> create ( $ createDataset ) ; $ this -> update ( $ updateDataset , $ condFields , $ updateNulls ) ; // Event $ this -> triggerEvent ( 'onAfter' . ucfirst ( __FUNCTION__ ) , [ 'result' => & $ dataset , ] ) ; return $ dataset ; }
Save will auto detect is conditions matched in data or not.
If matched, using update, otherwise we will create it as new record.

@param mixed $dataset      The data set contains data we want to save.
@param array $condFields   The where condition tell us record exists or not, if not set,
will use primary key instead.
@param bool  $updateNulls  Update empty fields or not.

@return  mixed|DataSet Saved data set.
Save will auto detect is conditions matched in data or not . If matched using update otherwise we will create it as new record .