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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

How to call a method on a data object


Go to End


14 Posts   6126 Views

Avatar
svinkle

Community Member, 16 Posts

17 January 2009 at 5:47am

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?

Avatar
svinkle

Community Member, 16 Posts

17 January 2009 at 5:56am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

17 January 2009 at 6:28am

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.

Avatar
svinkle

Community Member, 16 Posts

17 January 2009 at 8:01am

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.

Avatar
dio5

Community Member, 501 Posts

21 January 2009 at 2:41am

It seems to work for me on 2.2.3 if that helps :)

Avatar
svinkle

Community Member, 16 Posts

21 January 2009 at 5:37am

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.

Avatar
dio5

Community Member, 501 Posts

21 January 2009 at 9:29am

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?

Avatar
svinkle

Community Member, 16 Posts

22 January 2009 at 6:15am

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.

Go to Top