Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » many Attachments on FileDataObjectManager

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 3556 Views
  • jmax
    Avatar
    Community Member
    17 Posts

    many Attachments on FileDataObjectManager Link to this post

    Hi all from Italy and sorry for my english ;-)

    I have a problem with FileDataOjectManager. I'd like to have many attachment files, is it possible?

    the code with a single attachment is like this

    $manager = new FileDataObjectManager(
             $this, // Controller
             'Resources', // Source name
             'Resource', // Source class
             'Attachment', // File name on DataObject
             array(
                'Name' => 'Name',
                'Description' => 'Description',
                'Category' => 'Category',
                'Attachment.Name' => 'Attachment',
             ), // Headings
             'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
             // Filter clause
             // Sort clause
             // Join clause
          );

    but i need to have more than one file for each entries.

    like this:

    Name --- Description --- Category --- Attachment1 --- Attachment2 --- Attachment3

    Is it possible.

    Thanks to all

    Bye

    Max

  • UncleCheese
    Avatar
    4085 Posts

    Re: many Attachments on FileDataObjectManager Link to this post

    You can do a nested FileDataObjectManager

    class SomePage extends Page {
    has_many FileObjects => File Object

    getCMSFields() {
    new DataObjectManager($this, FileObjects, FileObject)
    }
    }

    class FileObject extends DataObject {

    has_one SomePage => SomePage

    has_many Files => MyFile

    getCMSFields {
    new AssetManager($this, Files, MyFile)
    }

    }

    class MyFile Extends File {
    has_one = FileObject => FileObject
    }

  • jmax
    Avatar
    Community Member
    17 Posts

    Re: many Attachments on FileDataObjectManager Link to this post

    hi UncleCheese,

    thanks for your answer, but it's the first time that i use the dataobjectmanager can you help me?

    this is my Resouce.php

    <?php
    class Resource extends DataObject
    {
       static $db = array (
          'Name' => 'Text',
          'Description' => 'Text',
          'Category' => "Enum('Industry, Finance, Education')",
       );
       
       static $has_one = array (
          'Attachment' => 'File',
          'ResourcePage' => 'ResourcePage'
       );
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Name'),
             new TextareaField('Description'),
             new DropdownField('Category','Category', singleton('Resource')->dbObject('Category')->enumValues()),
             new FileUploadField('Attachment')

          );
       }
    }
    ?>

    ==================================

    and this is the ResourcePage.php

    <?php
    class ResourcePage extends Page
    {
       static $has_many = array (
          'Resources' => 'Resource'
       );
       
       public function getCMSFields()
       {
          $f = parent::getCMSFields();
          $manager = new FileDataObjectManager(
             $this, // Controller
             'Resources', // Source name
             'Resource', // Source class
             'Attachment', // File name on DataObject
             array(
                'Name' => 'Name',
                'Description' => 'Description',
                'Category' => 'Category',
                'Attachment.Name' => 'Attachment',
             ), // Headings
             'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
             // Filter clause
             // Sort clause
             // Join clause
          );
          
          $manager->setFilter(
             'Category', // Name of field to filter
             'Filter by Category', // Label for filter
             singleton('Resource')->dbObject('Category')->enumValues() // Map for filter (could be $dataObject->toDropdownMap(), e.g.)
          );
          
          // If undefined, all types are allowed. Pass with or without a leading "."      
          $manager->setAllowedFileTypes(array('pdf','doc'));
          
          // Label for the upload button in the popup
          $manager->setBrowseButtonText("Upload (PDF or DOC only)");
          
          // In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
          $manager->setGridLabelField('Name');
          
          // Plural form of the objects being managed. Used on the "Add" button.
          // If left out, this defaults to [MyObjectName]s
          $manager->setPluralTitle('Resources');
                
          $f->addFieldToTab("Root.Content.Resources", $manager);

          return $f;
       }

    }

    class ResourcePage_Controller extends Page_Controller
    {
    }
    ?>

    Sorry but i don't know how to create the nested FileDataObjectManager that you write.
    I hope you can help me..

    Thank you very much

    Max

  • jmax
    Avatar
    Community Member
    17 Posts

    Re: many Attachments on FileDataObjectManager Link to this post

    Hi UncleCheese,
    i thank you so much for your answer, but can you help me more
    with your experience?

    thanks so much

  • UncleCheese
    Avatar
    4085 Posts

    Re: many Attachments on FileDataObjectManager Link to this post

    Try it this way:

    <?php
    class Resource extends DataObject
    {
    static $db = array (
    'Name' => 'Text',
    'Description' => 'Text',
    'Category' => "Enum('Industry, Finance, Education')",
    );

    static $has_many = array (
    'Attachments' => 'ResourceFile'
    );

    static $has_one = array (
    'ResourcePage' => 'ResourcePage'
    );

    public function getCMSFields_forPopup()
    {
    return new FieldSet(
    new TextField('Name'),
    new TextareaField('Description'),
    new DropdownField('Category','Category', singleton('Resource')->dbObject('Category')->enumValues()),
    new MultipleFileUploadField('Attachments')

    );
    }
    }

    ==================================

    ResourceFile.php

    <?php

    class ResourceFile extends File {

    static $has_one = array (
    'Resource' => 'Resource'
    );
    }

    ==================================
    and this is the ResourcePage.php

    <?php
    class ResourcePage extends Page
    {
    static $has_many = array (
    'Resources' => 'Resource'
    );

    public function getCMSFields()
    {
    $f = parent::getCMSFields();
    $manager = new DataObjectManager(
    $this, // Controller
    'Resources', // Source name
    'Resource', // Source class
    array(
    'Name' => 'Name',
    'Description' => 'Description',
    'Category' => 'Category',
    'Attachment.Name' => 'Attachment',
    ), // Headings
    'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
    // Filter clause
    // Sort clause
    // Join clause
    );

    $manager->setFilter(
    'Category', // Name of field to filter
    'Filter by Category', // Label for filter
    singleton('Resource')->dbObject('Category')->enumValues() // Map for filter (could be $dataObject->toDropdownMap(), e.g.)
    );

    // If undefined, all types are allowed. Pass with or without a leading "."
    $manager->setAllowedFileTypes(array('pdf','doc'));

    // Label for the upload button in the popup
    $manager->setBrowseButtonText("Upload (PDF or DOC only)");

    // In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
    $manager->setGridLabelField('Name');

    // Plural form of the objects being managed. Used on the "Add" button.
    // If left out, this defaults to [MyObjectName]s
    $manager->setPluralTitle('Resources');

    $f->addFieldToTab("Root.Content.Resources", $manager);

    return $f;
    }

    }

    class ResourcePage_Controller extends Page_Controller
    {
    }
    ?>

  • jmax
    Avatar
    Community Member
    17 Posts

    Re: many Attachments on FileDataObjectManager Link to this post

    Thank you very much!!
    You're Great!

  • jmax
    Avatar
    Community Member
    17 Posts

    Re: many Attachments on FileDataObjectManager Link to this post

    sorry Unclecheese,

    now in frontend i can load data of my DataObjectManager

    but how to link file uploaded?

    <% control Resources %>
    $Name - $Description - $Attachments
    <% end_control %>

    Before i was used Attacment.URL for a single file..

    but now don't work

  • UncleCheese
    Avatar
    4085 Posts

    Re: many Attachments on FileDataObjectManager Link to this post

    <% control Attachments %>

    3556 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.