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

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


Go to End


4 Posts   1328 Views

Avatar
insomniacnz

Community Member, 3 Posts

21 November 2010 at 10:56pm

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

Community Member, 3 Posts

22 November 2010 at 4:53am

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

Forum Moderator, 5523 Posts

23 November 2010 at 9:42pm

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

Community Member, 3 Posts

25 November 2010 at 4:50am

Edited: 25/11/2010 4:54am

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 "";
}