Dejsa Cocan
Dejsa Cocan

Reputation: 1569

Calling to data object function on upload (SilverStripe)

I have a DataObject class called AdminUpload that stores two variables: UploadDate (which is always going to bet set to the current date) and Total, which is an int.

The function updateUploads() is called and stores the current date and increments the Total by 1 each time its called.

<?php

class AdminUpload extends DataObject {

    private static $db = array(
        'UploadDate' => 'Date',
        'Total' => 'int'
    );

    // Tell the datagrid what fields to show in the table
    private static $summary_fields = array(
        'ID' => 'ID',
        'UploadDate' => 'Current Date',
        'Total' => 'Version Number'
    );

    public function updateUploads(){
        $uploadDate = SS_Datetime::now();
        $this->UploadDate = $uploadDate;
        $this->Total++;//increment the value currently stored in the database each time

        $this->write();
    }

}

What I want to to is, when someone uploads a new image in the admin view, then the updateCache() function is called during the onAfterWrite() process. I only want to maintain one entry in the database, though, so every time I upload an image, I want to have just one entry in the AdminUpload database table.

public function onAfterWrite(){
        $updateGallery = parent::onAfterWrite();

        $adminUploading = AdminUpload::get();
        $adminUploading -> updateUploads();

        return $updateGallery;
    }

I've never tried to do a function call in SilverStripe like this--it seems simple enough but since I am not going to add a new entry to the database with each call to the updateUploads() function, that's where I'm stuck. Any tips would be helpful...

Upvotes: 0

Views: 104

Answers (1)

Greg Smirnov
Greg Smirnov

Reputation: 1592

It is incorrect approach to create a whole table for just one record. If you were going to use theses two fields on a page, then adding them to that page (create a new page type) would be a better idea.

If you are talking about file uploads, then you can always query this information directly from database.

$uploadedFilesCount = File::get()->count();
$lastUploadedFileDate = File::get()->sort('CreatedDate', 'DESC')->first()->CreatedDate;

onAfterWrite is a hook and used from DataExtension. There are cases when hooks are called directly on DO and then on extensions.

Your extension code might look like this to handle 'onCreated' state:

class UploadsCounter extends DataExtension {
    protected $isCreating = false;

    public function onBeforeWrite() {
       if (!$this->owner->isInDB()) {
           $this->isCreating = true;
       }
    }

    // called on validation or database error
    public function onAfterSkippedWrite() {
        $this->isCreating = false;
    }

    public function onAfterWrite() {
        if (!$this->isCreating) return;

        $this->isCreating = false;
        $adminUploading = AdminUpload::get()->first();

        if (!$adminUploading ) {
            $adminUploading = new AdminUpload();
            $adminUploading->write();
        }

        $adminUploading->updateUploads();
    }
}

You should define UploadsCounter extension on the dataobject that you are going to count, for example:

mysite/_config/config.yml

File:
  extensions:
    - UploadsCounter

Upvotes: 2

Related Questions