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

[solved] Initialising a DataObject


Go to End


5 Posts   1434 Views

Avatar
Silverfish

Community Member, 59 Posts

28 November 2011 at 11:20pm

Hi All,

I'm trying to set some vars on initialising a subclass of a DataObject, i.e.

 class Test extends DataObject { .. 

but if I do

public function init() { .. } 
it's not called and if I do
public function Test() {..}
or
public function __construct() {..}
I get an "Undefined Index Error" from sapphire/core/ClassInfo.php

I guess I'm missing something really stupid but searching the forum and Google didn't help.

So thanks for any hint! :)

Regards
SF

Avatar
swaiba

Forum Moderator, 1899 Posts

29 November 2011 at 12:03am

Avatar
Willr

Forum Moderator, 5523 Posts

29 November 2011 at 7:37pm

If you call __construct on your data object make sure you call parent::__construct() as well!

Avatar
Silverfish

Community Member, 59 Posts

29 November 2011 at 10:31pm

Hi Willr,

didn't I say i miss something stupid? ;)

Thanks a lot, calling the parent constructor was the point, of course.

Regards
Sf

Avatar
Silverfish

Community Member, 59 Posts

3 December 2011 at 12:59am

Edited: 03/12/2011 1:00am

Just wanted to add that it's (obviously) quite important to call the parent constructor with all it's variables, like

public function __construct($record = null, $isSingleton = false) {

	parent::__construct($record, $isSingleton);
}

otherwise it will return empty Objects only ;).

Regards SF