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.

General Questions

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

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

Trying to get a file's URL but "Fatal error: Call to a member function"


Reply

4 Posts   810 Views

Avatar
insomniacnz

21 November 2010 at 10:56pm Community Member, 3 Posts

I'm a bit of a novice with PHP and silverstripe. I want to reference a file object's URL to pass it onto a form.

Relevant code snippets:

static $db = array(
"TermsAndConditions" => 'HTMLText'
);

static $has_one = array(
'TermsAndConditionsFile' => 'File'
);

And then in the controller:

function CreateForm() {
return new CreateForm($this, 'CreateForm', $this->TermsAndConditions, $this->TermsAndConditionsFile->getURL());
}

I know there is something fundamentally wrong with the '->getURL()' bit, how should I do this instead? Much appreciated if you can help!

Avatar
insomniacnz

22 November 2010 at 4:53am Community Member, 3 Posts

Phew..this seems to work.. but is it good practice to just append "ID" to a has_one array's name? I still don't quite understand but happy it works.

function CreateForm() {
return new CreateForm($this, 'CreateForm', $this->TermsAndConditions, $this->getTermsAndConditionsURL());
}

function getTermsAndConditionsURL() {
if ($tc = DataObject::get_by_id("File", "$this->TermsAndConditionsFileID"))
return $tc->URL;
else
return "";
}

Avatar
Willr

23 November 2010 at 9:42pm Forum Moderator, 5511 Posts

SS automatically adds a database column with ID on it so by calling $this->TermsAndConditionsFileID you are looking up the column in the database (which can be null), the problem with the orignal code ($this->TermsAndConditionsFile->getURL()) is that $this->TermsAndConditionsFile may return null and therefore getUrl() is undefined. You would have to check to see if $this->TermsAndConditionsFile() existed before calling getUrl.

Avatar
insomniacnz

25 November 2010 at 4:50am (Last edited: 25 November 2010 4:54am), Community Member, 3 Posts

Thanks Willr. If you have time, what is wrong with doing it this way, it comes up empty.

function getTermsAndConditionsURL() {
if ($tc = $this->TermsAndConditionsFile)
return $tc->URL;
else
return "";
}

EDIT: Ah hah! When I add a () when referencing the has_one array object it works! Didn't know that until i noticed it in your post :)

function getTermsAndConditionsURL() {
if ($tc = $this->TermsAndConditionsFile())
return $tc->URL;
else
return "";
}