Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » ManyManyDataObjectManager - Unable to Use Function Call $field_names Array?

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 509 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    ManyManyDataObjectManager - Unable to Use Function Call $field_names Array? Link to this post

    Hi,

    I've been working with the DataObjectManager module for years, but today I came across a strange error (probably a mistake in my code that i can't see). I have a ManyManyDataObjectManager in my generic Page class like so:

    <?php

    class Page extends SiteTree {

       .....
       
       static $many_many = array(
          "Buttons" => "Button"
       );
       
       function getCMSFields() {
       
          $fields = parent::getCMSFields();
       
          $buttons = new ManyManyDataObjectManager(
             $this,
             "Buttons", // relation name
             "Button", // object class
             Button::$field_names, // fields to show in table
             Button::getCMSFields_forPopup(), // form that pops up for edit
             "", // a filter to only display item associated with this page
             "SortOrder ASC" // Sort
          );
          $fields->addFieldToTab("Root.Content.Buttons", $buttons);
       
          return $fields;
       }

    }

    And here is my Button DataObject:

    <?php

    class Button extends DataObject {

       static $db = array(
          "Title" => "Text"
       );
       
       static $has_one = array(
        "LinkedPage" => "SiteTree"
       );
       
       static $belongs_many_many = array(
          "Pages" => "Page"
       );
       
       static $field_names = array(
          "Title" => "Title",
          "PageName" => "Page Name"
       );
       
       function getCMSFields_forPopup() {

          $fields = new FieldSet();
          
          $fields->push(new TextField("Title", "Text"));
          
          $LinkedPageField = new SimpleTreeDropdownField("LinkedPageID", "Link to Page", "SiteTree");
          $LinkedPageField->setEmptyString("-- None --");
          $fields->push($LinkedPageField);

          return $fields;

       }

       public function PageName() {
          $PageObject = DataObject::get_by_id("SiteTree", $this->LinkedPageID);
          return ($PageObject!=null) ? $PageObject->Title : "";
       }
       
    }

    The error when I click on a page in the CMS is:

    Unknown column 'PageName' in 'field list'

    Ha? I thought I could put function names in this associative array. It isn't meaningful to put the LinkedPageID itself in the display table, which is why I am calling PageName() instead. Indeed, I can't use LinkedPageID here either or I get this error:

    Duplicate column name 'LinkedPageID'

    What am I doing wrong here?

    Thanks in advance,
    Garrett

    509 Views
Page: 1
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.