octobercmsoctobercms-backendoctober-form-controller

Find the Model name of the field/s that has been updated in Backend\Behaviors\FormController using OctoberCMS


I want to know the Model Name of the field/s that has been updated in Backend\Behaviors\FormController class in OctoberCMS backend here is the picture enter image description here

This is the update_onSave() function that will trigger after the form is updated in my own plugin this is located in Backend\Behaviors\FormController

/**
 * AJAX handler "onSave" called from the update action and
 * primarily used for updating existing records.
 *
 * This handler will invoke the unique controller overrides
 * `formBeforeUpdate` and `formAfterUpdate`.
 *
 * @param int $recordId Record identifier
 * @param string $context Form context
 * @return mixed
 */
public function update_onSave($recordId = null, $context = null)
{
    $this->context = strlen($context) ? $context : $this->getConfig('update[context]', self::CONTEXT_UPDATE);
    $model = $this->controller->formFindModelObject($recordId);
    $this->initForm($model);

    $this->controller->formBeforeSave($model);
    $this->controller->formBeforeUpdate($model);

    $modelsToSave = $this->prepareModelsToSave($model, $this->formWidget->getSaveData());
    Db::transaction(function () use ($modelsToSave) {
        foreach ($modelsToSave as $modelToSave) {
            $modelToSave->save(null, $this->formWidget->getSessionKey());
        }
    });

    $this->controller->formAfterSave($model);
    $this->controller->formAfterUpdate($model);

    Flash::success($this->getLang("{$this->context}[flashSave]", 'backend::lang.form.update_success'));

    if ($redirect = $this->makeRedirect('update', $model)) {
        return $redirect;
    }
}

This is the formAfterUpdate() located also in Backend\Behaviors\FormController that will trigger after the updating form is saved.

 /**
 * Called after the updating form is saved.
 * @param Model
 * 
 */
public function formAfterUpdate($model)
{
    echo $model;

}

I want to know the Model name from my plugin that has been updated but it only displays the fields that has been updated like this.

{"flagstateid":80,"name":"VIETNAM","code":"[VN]","image":null}

Solution

  • I guess its more simple then we thought

    if you are currently working on controller which implement Backend\Behaviors\FormController or even inside Backend\Behaviors\FormControlleryou can get Model Class\name like this

    /**
     * Called after the updating form is saved.
     * @param Model
     */
    public function formAfterUpdate($model)
    {
        $modelName = $this->config->modelClass;
        // $modelName will be : "\HardikSatasiya\TimeTracker\Models\TimeLog"
    }
    

    As simply form Behavior require config to update model so you can get that data from config it-self

    Check out this image

    enter image description here

    if you have any doubt please comment.