Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to call a method on a data object

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

Page: 1 2
Go to End
Author Topic: 2992 Views
  • svinkle
    Avatar
    Community Member
    16 Posts

    How to call a method on a data object Link to this post

    Regarding thread #143123 (http://www.silverstripe.org/archive/show/143123),

    How do I call a method on a data object? In willr's response, he does not call the method anywhere in the data object. How does this work?

  • svinkle
    Avatar
    Community Member
    16 Posts

    Re: How to call a method on a data object Link to this post

    Here's another post with the same issue. http://www.silverstripe.org/archive/show/117959

  • UncleCheese
    Avatar
    4085 Posts

    Re: How to call a method on a data object Link to this post

    Yeah, that's kind of tricky.

    He defines the method getImageFileName(), and you'll see in the table, he sets up a field for ImageFileName in the table headings array. If Silvesrtripe doesn't find a property or field called ImageFileName, it knows to try running a method called getImageFileName.

    It's all done with the wildcard methods in PHP5, __get() and __call(). Very cool stuff.

  • svinkle
    Avatar
    Community Member
    16 Posts

    Re: How to call a method on a data object Link to this post

    Hi UncleCheese,

    Thanks for the information. It has helped me to understand what is happening here.

    Unfortunately I still have an issue. I have everything set up correctly but the method does not return anything. Not even a simple string return for testing. I think I'm at the same point as described in thread #117959 linked above.

    Why would there be no return value?
    Thank you.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: How to call a method on a data object Link to this post

    It seems to work for me on 2.2.3 if that helps

  • svinkle
    Avatar
    Community Member
    16 Posts

    Re: How to call a method on a data object Link to this post

    Hi dio5,

    That is interesting. The version I'm working with is 2.3 rc2. It makes me nervous to potentially use this version in a production environment if something as basic as calling a method on a data object doesn't work.

    Here's an example of my code:

    class LinksTable extends Page {

    static $many_many = array(
    'LinksTable1' => Links',
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();

    $LinksTable1tablefield = new ManyManyComplexTableField(
    $this,
    'LinksTable1',
    'Links',
    array(
    'LinkName' => 'Link Name',
    'LinkTitle' => 'Link Title',
    'LinkLocation' => 'Link URL',
    'LinkColumnName' => 'Link Column'
    ),
    'getCMSFields_forPopup'
    );
    $LinksTable1tablefield->setAddTitle( 'New Link' );
    $LinksTable1tablefield->setPageSize(25);

    return $fields;
    }

    }

    The field in question is "LinkColumnName". In the database this data is stored as type 'Int'. I want to display a text value on output, depending on the return value.

    The function being called, as it is right now:

    class Links extends DataObject {
    static $db = array(
    'LinkName' => 'Text',
    'LinkTitle' => 'Text',
    'LinkLocation' => 'Text',
    'LinkColumn' => 'Int'
    );

    static $has_one = array(
    );

    static $belongs_many_many = array(
    'LinksTable1' => 'LinksTable'
    );

    function getCMSFields_forPopup() {
    $options = array('1'=>'Column One','2'=>'Column 2','3'=>'Column 3');

    $fields = new FieldSet();
    $fields->push( new TextField( 'LinkName', 'Link Name' ) );
    $fields->push( new TextField( 'LinkTitle', 'Link Title' ) );
    $fields->push( new TextField( 'LinkLocation', 'Link URL' ) );
    $fields->push( new DropdownField('LinkColumn', 'Link Column', $options));
    return $fields;
    }

    function getLinkColumnName() {
    return "test";
    }

    }

    This should return "test" in each row of the "LinkColumnName" column, but it returns nothing.

    Any insight would be appreciated.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: How to call a method on a data object Link to this post

    Hmm, would indeed expect this to work (unless I overlooked something).

    Any chance you could try this on a 2.2.3 to make sure it's the version difference?

  • svinkle
    Avatar
    Community Member
    16 Posts

    Re: How to call a method on a data object Link to this post

    Hi dio5,

    I tried my code in 2.2.3 today with no success. The same results occurred with blank fields.

    Would it be possible for you to share a code sample which works in 2.2.3?

    Thanks.

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