Jump to:

23487 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Trying to get a file's URL but "Fatal error: Call to a member function"

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

Page: 1
Go to End
Author Topic: 802 Views
  • insomniacnz
    Avatar
    Community Member
    3 Posts

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

    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!

  • insomniacnz
    Avatar
    Community Member
    3 Posts

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

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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

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

    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.

  • insomniacnz
    Avatar
    Community Member
    3 Posts

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

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

    802 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.