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

Auto-populate column within a Dataobject


Go to End


11 Posts   1476 Views

Avatar
nzstephenf

Community Member, 63 Posts

4 December 2014 at 5:16pm

Hey Community!
Need some help with this little problem that involves Dataobjects and adding new columns with no values.

I've added a new column called "URLSegment" on this DataObject called EventSession, and this is generated by the Title of a different DataObject called Event. This works perfectly fine. But the thing is that this only applies the rule to add URLSegment onto future entries. Previous entries entered into the database for the dataobject EventSession are NULL and do not have a generated URL Segment.

Is there any way to auto-populate this column on the EventSession table that follows the same rule of generating a URLSegment based from the other dataobject 'Event'.

Cheers,
Stephen

Avatar
camfindlay

Forum Moderator, 267 Posts

22 December 2014 at 11:11am

Might need to see some code to get what you're trying to do :)

Avatar
nzstephenf

Community Member, 63 Posts

23 December 2014 at 1:14am

Edited: 23/12/2014 1:15am

Heya so here is what I've got:

EventSession.php

class EventSession extends DataObject {
    private static $db = array(
        "URLSegment"                => "Varchar(250)"
    );

   private static $has_one = array(
        "Event"                     => "Event"
    );
}

Event.php

class Event extends DataObject {
	
    private static $db = array(
        "Title"                     => "Varchar(250)",
        "Description"               => "Text"
    );
	
    private static $has_many = array(
        "EventSessions"             => "EventSession"
    );
}

Is there a way to populate things of EventSession.php's DB field "URLSegment" that is generated by Event.php's Title? At the moment, any dataobject created now will generate the URLSegment now but anything before say like the 10th of December will be empty and null. :)

Avatar
camfindlay

Forum Moderator, 267 Posts

23 December 2014 at 8:09am

Would the EventSession URLSegment need to be unique for each entry?

You could try using an onAfterWrite hook on Event class.

I take it you're trying to set up some sort of recurring events system?

Avatar
nzstephenf

Community Member, 63 Posts

23 December 2014 at 1:44pm

I suck sometimes at explaining things I think. Haha!

We have a recurring event system in place, but the thing we have a onAfterWrite already and it's working great with entries that come into the database now. I'm tryna see if there is a way if I could do something like a way to re-build the database and then have every "URLSegment" that is NULL in the EventSession database to have it's column populated based on the "Title" without having to enter every single entry and save changes for onAfterWrite to run.

Avatar
camfindlay

Forum Moderator, 267 Posts

23 December 2014 at 2:14pm

I see, a task is probably what you want, or you could simply create some SQL to run on the database once. The one think I'm not quite getting is the EventSession URLSegment I assume needs to be unique per EventSession right? That might be a bit more tricky. Is the URLSegment on the Event Session used to display a page or something to that effect?

Avatar
nzstephenf

Community Member, 63 Posts

23 December 2014 at 6:42pm

Yeah the EventSession needs to be unique. Yup yup, the URLSegment is used to view the actual event thus why it needs to be unique :)

Avatar
camfindlay

Forum Moderator, 267 Posts

24 December 2014 at 11:36am

what is in your current onbeforewrite function?

So what you are after is a one off task which will fill in any old records with the URLSegment AND make sure each EventSession URLSegment is unique PER parent Event?

Go to Top