Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » ?Bug? Overloading Getters

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

Page: 1
Go to End
Author Topic: 1614 Views
  • reekrd
    Avatar
    Community Member
    9 Posts

    ?Bug? Overloading Getters Link to this post

    Hi.

    I've added two extra fields to my HomePage. Demo and Demo2. Both are HTMLText. Before I output any data in those fields I need to change all & signs to & or I'll get an invalid XML error. I don't want to escape all HTML entities since the data I store in those fields are HTML snippets with an embed code from youtube.

    In my HomePage.php file I added this.

    function getDemo() {
          return str_replace('&','&',$this->Demo);
    }

    and in HomePage.ss I just call it using $Demo

    But nothing is returned.

    I changed the function to

    function getDemo() {
          return "ABC";
    }

    and now ABC is returned.

    Changed again to

    function getDemo() {
          return "ABC" . $this->Demo;
    }

    now the output is ABCABC

    I'm not sure if I'm logically doing something wrong. I read this http://doc.silverstripe.com/doku.php?id=datamodel and there they are using $this->Status so I'm guessing I'm doing it right but it still seems like $this->Demo ends up calling getDemo instead of giving me the value stored in Demo.

    If I change the function to return $this->Demo2 or if I rename the function to getMyDemo() then it works but then what's the point of overloading it?

    Any thoughts or maybe even better any idea on what would be the best way to allow users to copy and paste a youtube embed snippet without them having to change the & to & manually?

    Cheers

  • UncleCheese
    Avatar
    4085 Posts

    Re: ?Bug? Overloading Getters Link to this post

    What you're trying to do is supported natively by the XML_val function. $this->XML_val('Demo');

    Weird that you're using an HTMLText datatype to take a URL. Why not just use a VarChar?

    I would skip the getDemo() all together and just overload the Demo property. The template will look for a function first.

    function Demo()
    {
    return str_replace('&','&',$this->Demo);
    }

  • reekrd
    Avatar
    Community Member
    9 Posts

    Re: ?Bug? Overloading Getters Link to this post

    UncleCheese thanks for your reply.

    I'm bit lost when it comes to the different datatypes and which one to use. I'll change it to a normal text field as setting the field like this:
    'Demo' => 'Varchar' creates a Varchar(50) in the database and that is not enough to store a youtube embed code.

    I'm not using $Demo.XML (which I assume does the same thing as XML_val) because that makes turns everything into HTML entities and the embed code shows up as text instead of actual HTML element on the site.

    This is what I want to put into a field in the CMS and when I show it on my homepage I want the video to be there:

    <object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/tWNaJrbPJZk&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/tWNaJrbPJZk&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>

    I'm not sure I understand the exact difference between overloading getDemo() and Demo() but when I overload the function Demo() it works. Thanks!

  • UncleCheese
    Avatar
    4085 Posts

    Re: ?Bug? Overloading Getters Link to this post

    Yeah, try casting it as Text. You shouldn't need that str_replace stuff.

    1614 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.