add
add

Reputation: 111

How to attach a file to a dataobject in silverstripe 3

I have a simple task that I can't seem to figure out. In SS3 how can I attach a file to my data object or in other words create a file upload field, and bind that file object to the data object. See example below:

class myDataObject extends DataObject{

 public static $db = array(
    'Title' => 'Varchar',
    'Desc' => 'Text',
    'Help Text' => 'HTMLText',
    // 'File Upload (document)' => ???
 );

}//class

Note: For now I'd like to do a single file only, but later, on another object I'd like to do multiple files.

Upvotes: 1

Views: 855

Answers (2)

wmk
wmk

Reputation: 4626

File cannot be set in $db but in $has_one, as it's a relation to another DataObject.

So you'd need:

private static $has_one = array(
  'FileUpload' => 'File'
);

I don't think spaces and brackets in the $db or $has_one keys are a good idea, as they are used for database fields. If you want to set a title for the scaffolded fields please use $field_labels.

private static $field_labels = array(
  'Title' => 'My fancy title',
  'Desc' => 'Description',
  'has_one_FileUpload' => 'File upload (document)'
);

If you want mutliple files you'd need to define it as $has_many insetead of $has_one.

See http://doc.silverstripe.org/framework/en/tutorials/5-dataobject-relationship-management for more information.

Upvotes: 7

drawde83
drawde83

Reputation: 139

'File Upload (document)' => 'File'

In the CMS this will give you fields to add files to the cms. File Class

Upvotes: 0

Related Questions