Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detach in ModelAdmin #39

Open
JayDevlin opened this issue Mar 20, 2012 · 0 comments
Open

Detach in ModelAdmin #39

JayDevlin opened this issue Mar 20, 2012 · 0 comments

Comments

@JayDevlin
Copy link

If there is a FileAttachmentField in ModelAdmin the detach method won't work as expected. You will need to click detach & save twice as in "Detach => ID=0 => Save => Image reappears=> Detach => Save => Image detached".

This seems to be only the case in ModelAdmin.

I've traced the problem to FileAttachmentField->setValue().
https://github.com/unclecheese/KickAssets/blob/master/code/FileAttachmentField.php#L35

// if button detach clicked then ID = 0, so this condition fails
if($id = Controller::curr()->getRequest()->requestVar($this->Name()."ID")) {
    $value = $id;
}
// in ModelAdmin: the old DataObject will overwrite the currently detached file
elseif(!$value && $data && $data instanceof DataObject && $data->hasMethod($this->name)) {
    $funcName = $this->name;
    if($obj = $data->$funcName()) {
        if($obj instanceof File) {
            $value = $obj->ID;
        }
    }
}

My fix is to check requestVars with isset():

$requests = Controller::curr()->getRequest()->requestVars();
if( isset($requests[$this->Name()."ID"])) {
    $value = $requests[$this->Name()."ID"];
}
elseif(!$value && $data && $data instanceof DataObject && $data->hasMethod($this->name)) {
    $funcName = $this->name;
    if($obj = $data->$funcName()) {
        if($obj instanceof File) {
            $value = $obj->ID;
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant