Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

ManyManyDataObjectManager - Unable to Use Function Call $field_names Array?


Reply


683 Views

Avatar
Garrett

Community Member, 245 Posts

12 July 2012 at 5:49am

Edited: 12/07/2012 5:52am

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