Skip to content
Browse files

ENHANCEMENT: Allow programmers to set OwnerID for new files.

NOTE: This change should be reviewed to make sure it does not cause any side effects.

Example use case: 
    An admin porting user images from an old website.
    The script would put the images in the assets folder,
    and then insert them into the database using Image::write()
    and data from the old database.

public function insertImage($data) {
    $image = new Image();
    $image->ParentID = $data->parentId;
    $image->Title = $data->title;
    $image->FileName = $data->filename;
    $image->OwnerID = $data->ownerId;
    $image->write();
    
    // In the current version, this results in all images
    // being owned by Member::currentUser() instead of 
    // the expected $data->ownerId;
}
  • Loading branch information...
1 parent a9bf7df commit 3d9e12baf43535c82e41771138307ceb61630309 @CheeseSucker CheeseSucker committed
Showing with 3 additions and 1 deletion.
  1. +3 −1 filesystem/File.php
View
4 filesystem/File.php
@@ -466,7 +466,9 @@ protected function onBeforeWrite() {
parent::onBeforeWrite();
// Set default owner
- if(!$this->ID) $this->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
+ if(!$this->ID && !$this->OwnerID) {
+ $this->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
+ }
// Set default name
if(!$this->getField('Name')) $this->Name = "new-" . strtolower($this->class);

0 comments on commit 3d9e12b

Please sign in to comment.
Something went wrong with that request. Please try again.