Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObjectManager Code Examples

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3 4 5 6 7 8 9 10 11
Go to End
Author Topic: 20909 Views
  • stevanovich
    Avatar
    Community Member
    60 Posts

    Re: DataObjectManager Code Examples Link to this post

    Uncle cheese I have changed page with no luck nothing is happening with regards to sorting the datasheets I have into categories.

    Here is the code may help??

    All the objects are working fine just no Category sorting or title.

    ----------------
    ResourcePage.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'
    ), // 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;
    }

    function MyFileObjects()
    {
    $filter = Director::urlParam('Action');
    return $filter ? $this->Resources("Category = '$filter'") : $this->Resources();
    }

    }
    class ResourcePage_Controller extends Page_Controller

    {
    function category()
    {
    $resources = DataObject::get("Resource","Category='".$this->urlParams['ID']."'");
    if($resources) {
    return $this->customise(array(
    'Resources' => $resources,
    'CategoryTitle' => $this->urlParams['ID']
    ));
    }
    return false;
    }
    }

    -----------------
    Resource.php
    ----------------

    class Resource extends DataObject
    {
       static $db = array (
          'Name' => 'Text',
          'Description' => 'Text',
          'Category' => "Enum('CSO Storage Solutions, Storm Water Storage Solutions, Highways Applications, Surface Water & Sewage Pipelines, Water Treatment Works, Drinking Water, Renewable Energy, ATS Applications, Litespeed Applications')"
       );
       
       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 FileIFrameField('Attachment')
          );
       }
    }

    --------------
    ResourcePage.ss
    --------------

    <% if Resources %>
    <% control Resources %> $CategoryTitle
    <ul>

    <li><a href="$Attachment.URL">$Name</a><br>$Description</li>

    </ul>

       <% end_control %>
    <% end_if %>

  • Richie
    Avatar
    Community Member
    18 Posts

    Re: DataObjectManager Code Examples Link to this post

    I followed your video about nested DOM but I am missing the EmployeePage.php
    I assume I need this to create the page but what to add to get this running?

    Thanks for the great work!

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Code Examples Link to this post

    As long as you have EmployeePage extends Page in EmployeePage.php in your code folder, and you run a /dev/build, you should get the choice in your create dropdown menu.

  • Richie
    Avatar
    Community Member
    18 Posts

    Re: DataObjectManager Code Examples Link to this post

    Now my EmployeePage.php looks like this:

    class EmployeePage extends Page
    class EmployeePage extends Page
    {
       static $has_many = array (
          'Employees' => 'Employee'
       );   
       
       public function getCMSFields()
       {
          $f = parent::getCMSFields();
          $manager = new DataObjectManager(
             $this,
             'Employees',
             'Employee',
             array('Name' => 'Name', 'JobTitle' => 'Job Title'),
             'getCMSFields_forPopup'
          );
          $f->addFieldToTab("Root.Content.Employees", $manager);
          return $f;
       }

    }


    This only shows the DOM without the nested DOMs info (Schools/Resumes).
    I'm doing something wrong, but what? Thanks again for your time.

    Edit: Added function name 'getCMSFields_forPopup' inside DOM.

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Code Examples Link to this post

    Where's your code for the Employee object?

  • Richie
    Avatar
    Community Member
    18 Posts

    Re: DataObjectManager Code Examples Link to this post

    You're fast! Here it is:

    class Employee extends DataObject
    {
       static $db = array (
          'Name' => 'Text',
          'JobTitle' => 'Text'
       );
       
       static $has_one = array (
          'EmployeePage' => 'EmployeePage'
       );
       
       static $has_many = array (
          'Schools' => 'School',
          'Resumes' => 'Resume'
       );   
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Name'),
             new TextField('JobTitle', 'Job Title'),
             new DataObjectManager(
                $this,
                'Schools',
                'School',
                array('Title' => 'Title of School', 'Degree' => 'Degree', 'GraduationYear' => 'Graduation Year')
             ),
             new FileDataObjectManager(
                $this,
                'Resumes',
                'Resume',
                'Attachment',
                array('Description' => 'Description')
             )
          );
       }
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Code Examples Link to this post

    You haven't passed a function name into the DOM, so it's assuming "getCMSFields()" as your popup method. Change:

    public function getCMSFields_forPopup()

    to

    public function getCMSFields()

  • Richie
    Avatar
    Community Member
    18 Posts

    Re: DataObjectManager Code Examples Link to this post

    Thanks for your help. I passed the function name into the DOM (see also my edited post above).

    20909 Views
Page: 1 2 3 4 5 6 7 8 9 10 11
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.