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

Basic DataObject Question


Go to End


3 Posts   665 Views

Avatar
KenSander

Community Member, 2 Posts

2 November 2012 at 4:57am

I'm a designer, so pretty newb when it comes to development logic, so these will be really simple questions as I try to learn a bit :)

I want a dataobject that will be included on every page. It will contain contact info that will be slightly different on certain pages. I want each page to only have 1 dataobject though many might exist.

Here is my setup:

class ContactInfo extends DataObject {
    static $db = array(
        'Title' => 'Varchar',
        'Info' => 'HTMLText'

    );

    static $belongs_many_many = array(
        'Pages' => 'Page'
    );

}

public static $many_many = array(
		'Info' => 'ContactInfo'
	);


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

        $contactInfo = new GridField(
            'ContactInfo',
            'Contact Information',
            $this->Info(),
            GridFieldConfig_RelationEditor::create()
        );

        $fields->addFieldToTab('Root.ContactInfo', $contactInfo);
        return $fields;
    }

Is this the proper way to set this up? I'm finding when I delete a dataobject on the page, it removes it completely and not just from this page. I was under the impression the GridFieldConfig_RelationEditor::create() would allow only the deletion from the page.

How do I limit it so each page only can include 1? The thing is, I'll have say 3 dataobjects that I want to use across all my pages, so some will be on more than 1 page. I don't want it unique to that page.

Avatar
copernican

Community Member, 189 Posts

2 November 2012 at 6:23am

to fix RelationEditor() from deleting the object do this. I believe this is a bug in 3.0.2 and will hopefully be corrected in the next release.

$gridFieldConfig = GridFieldConfig_RelationEditor::create();
$gridFieldConfig->removeComponentsByType('GridFieldDeleteAction');
$gridFieldConfig->addComponent(new GridFieldDeleteAction(true));
$contactInfo = new GridField( 
'ContactInfo', 
'Contact Information', 
$this->Info(), 
$gridFieldConfig     

I can't help on limiting it to only one though. I'm interested in learning that myself. If i do figure out a solution i'll be sure to post it here.

Avatar
KenSander

Community Member, 2 Posts

3 November 2012 at 6:30am

Oh okay thanks! I thought maybe my relationship was setup incorrect so I was pretty puzzled for a while. A bug makes sense.

And that would be great if you find anything about limiting it to 1. It isn't urgent for what I'm setting up now, but I think it would be a good feature to know moving forward.