protected function saveCustomFields(): bool
    {
        //find the custom field module
        if (!$module = Modules::findFirstByName($this->getSource())) {
            return false;
        }

        //we need a new instane to avoid overwrite
        $reflector = new \ReflectionClass($this);
        $classNameWithNameSpace = $reflector->getNamespaceName() . '\\' . $reflector->getShortName() . 'CustomFields';

        //if all is good now lets get the custom fields and save them
        foreach ($this->customFields as $key => $value) {
            //create a new obj per itration to se can save new info
            $customModel = new $classNameWithNameSpace();

            //validate the custome field by it model
            if ($customField = CustomFields::findFirst(['conditions' => 'name = ?0 and modules_id = ?1', 'bind' => [$key, $module->id]])) {
                //throw new Exception("this custom field doesnt exist");

                $customModel->setCustomId($this->getId());
                $customModel->custom_fields_id = $customField->id;
                $customModel->value = $value;
                $customModel->created_at = date('Y-m-d H:i:s');

                if (!$customModel->save()) {
                    throw new Exception('Custome ' . $key . ' - ' . $this->customModel->getMessages()[0]);
                }
            }
        }

        //clean
        unset($this->customFields);

        return true;
    }
protected function saveCustomFields(): bool
    {
        //find the custom field module
        if (!$module = Modules::findFirstByName($this->getSource())) {
            return false;
        }

        //we need a new instane to avoid overwrite
        $reflector = new \ReflectionClass($this);
        $classNameWithNameSpace = $reflector->getNamespaceName() . '\\' . $reflector->getShortName() . 'CustomFields';

        //if all is good now lets get the custom fields and save them
        foreach ($this->customFields as $key => $value) {
            //create a new obj per itration to se can save new info
            $customModel = new $classNameWithNameSpace();

            //validate the custome field by it model
            if ($customField = CustomFields::findFirst(['conditions' => 'name = ?0 and modules_id = ?1', 'bind' => [$key, $module->id]])) {
                //throw new Exception("this custom field doesnt exist");

                $customModel->setCustomId($this->getId());
                $customModel->custom_fields_id = $customField->id;
                $customModel->value = $value;
                $customModel->created_at = date('Y-m-d H:i:s');

                if (!$customModel->save()) {
                    throw new Exception('Custome ' . $key . ' - ' . $this->customModel->getMessages()[0]);
                }
            }
        }

        //clean
        unset($this->customFields);

        return true;
    }
protected function saveCustomFields ( ) : bool { //find the custom field module if ( ! $ module = Modules :: findFirstByName ( $ this -> getSource ( ) ) ) { return false ; } //we need a new instane to avoid overwrite $ reflector = new \ ReflectionClass ( $ this ) ; $ classNameWithNameSpace = $ reflector -> getNamespaceName ( ) . '\\' . $ reflector -> getShortName ( ) . 'CustomFields' ; //if all is good now lets get the custom fields and save them foreach ( $ this -> customFields as $ key => $ value ) { //create a new obj per itration to se can save new info $ customModel = new $ classNameWithNameSpace ( ) ; //validate the custome field by it model if ( $ customField = CustomFields :: findFirst ( [ 'conditions' => 'name = ?0 and modules_id = ?1' , 'bind' => [ $ key , $ module -> id ] ] ) ) { //throw new Exception("this custom field doesnt exist"); $ customModel -> setCustomId ( $ this -> getId ( ) ) ; $ customModel -> custom_fields_id = $ customField -> id ; $ customModel -> value = $ value ; $ customModel -> created_at = date ( 'Y-m-d H:i:s' ) ; if ( ! $ customModel -> save ( ) ) { throw new Exception ( 'Custome ' . $ key . ' - ' . $ this -> customModel -> getMessages ( ) [ 0 ] ) ; } } } //clean unset ( $ this -> customFields ) ; return true ; }
Create new custom fields

We never update any custom fields, we delete them and create them again, thats why we call cleanCustomFields before updates

@return void
Create new custom fields