Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using getEditForm() method for multiple managed models


Go to End


2 Posts   1066 Views

Avatar
Faloude

Community Member, 55 Posts

15 December 2016 at 4:26pm

Edited: 15/12/2016 4:27pm

Suppose you have a ModelAdmin with two managed models.

class MyAdmin extends ModelAdmin {

    private static $managed_models = array(
        'Product',
        'Category'
    );

    public function getEditForm($id = null, $fields = null) {
        $form = parent::getEditForm($id, $fields);

        $model = $this->sanitiseClassName($this->modelClass);

        // Sophisticated debug trick :-)
        die(var_dump($model));  // This will output string(7) "Product"

        return $form;
    }
}

I find it strange that $this->modelClass holds just one model and not both, which makes me wonder if getEditForm() works the same way as getCMSFields(). Does getEditForm() execution iterate over the given models?

The reason for asking is because eventually my goal is to merge the two seperated model gridfields under one tab and I'm not sure if I'm working in the right class method.

Avatar
Kirk

Community Member, 67 Posts

15 December 2016 at 5:18pm

If you look at the parent getEditForm from LeftAndMain you see it calls getCMSFields which is where any iteration would be done.
Also looking at the API $modelClass is supposed to be a string not a array.
To iterate over both models in getEditForm use self::$managed_models

I would say that getEditForm is the right method and I would try removing the gridfields from $form->Fields() and then compose my own GridField and push it back into $form->Fields()