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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[Solved] DataObjects and reusing them


Go to End


5 Posts   600 Views

Avatar
Vlad Belfort

Community Member, 55 Posts

11 May 2016 at 5:05am

I've ran into an issue with DataObjects which is giving me some behaviour I didn't expect.

So I have something called MailingList which adds a tab in the CMS and allows users to add in contact information which is then displayed in the template for that page.

It has just 2 input fields, email address and description. That's all that needs to be displayed.

It was working how I wanted it to when I was only using it in one place, but during the course of development I've started to add it to other page types and as a result it's now displaying things from the other page types along with the original email and description fields.

Is there a way to stop the DataObject from outputting something from everything it's connected to and just leave it displaying the fields I need?

Avatar
swaiba

Forum Moderator, 1899 Posts

11 May 2016 at 8:01pm

Easily done, add this in either individual subclasses, or at a certain point to ensure all it's children have it removed.

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

	$fields->removeByName(array('FieldName','RelationshipOrTabName'));

	return $fields;
}

Avatar
Vlad Belfort

Community Member, 55 Posts

11 May 2016 at 10:06pm

Edited: 11/05/2016 10:09pm

Hmm that doesn't seem to work...

class MailingList extends DataObject {

    private static $db = array(
        'Title' => 'Varchar(25)',
        'Description' => 'Varchar(55)'
    );

    private static $has_one = array(
        'Shows' => 'Shows',
        'Department' => 'Department'
    );

    public function getCMSFields(){

        $fields = parent::getCMSFields();

        $fields->removeByName('Shows'); # Doesn't remove shows, see screenshot

        return $fields;
    }
}

Edit
Just some more info, the Shows and Department have nothing to do with each other which is why I want to remove them, it's just that MailingList DataObject is used in both the page types.

Avatar
swaiba

Forum Moderator, 1899 Posts

11 May 2016 at 10:10pm

super choice! - yes for that case you'll find the name of the field is actually "ShowsID" - all has_ones have "ID" added to the end

Avatar
Vlad Belfort

Community Member, 55 Posts

11 May 2016 at 10:24pm

Fantastic! That worked :)

I was concerned that removeByName() would've just hidden the fields but still sent the info on submit but I checked the MailingList table and the ID of the page-type not being used is stored as 0 (Phew!)

Thanks for letting me know, I'll bare that in mind moving forward.