Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Extending Page with Image

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

Page: 1
Go to End
Author Topic: 1996 Views
  • NicolasLeuenberger
    Avatar
    Community Member
    9 Posts

    Extending Page with Image Link to this post

    Hi everyone,

    in Page.php I've defined that every page should contain an image, which works just fine.

    static $has_one = array(
    'Photo' => 'Image'
    );

    Now I want to extend that page type. The ChildPage should still contain an image and some additionnal stuff. The problem is, that the image definition does not get inherited. There is no "PhotoID" column in the childpage table.
    If I add

    static $has_one = array(
    'Photo' => 'Image'
    );


    to the child page, this column is created but no data is written into it on uploading an image to this page.
    Of course i could name the column differently, but then I run into problems when I want to display the image in my template.

    Is there a way to solve this problem?

    Cheers,
    Nicolas

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Extending Page with Image Link to this post

    That isn't a bug, it's by design. That's how relational databases work with foreign keys. You'll notice that there should be a PageID column in your subclass table (if you have any additional DB fields set up on that class). That is how they are linked, meaning you _shouldn't_ have a PhotoID on the subclassed table as well, or it becomes ambiguous.

    This is how a Page instance still has a Title, MenuTitle etc fields, even though the Page table doesn't have these columns.

    It still means that if you save an image field you can still reference $Photo from the subclass of Page.

    Provided you subclassed Page correctly, you should still get a field for uploading an image on it if you created an ImageField in getCMSFields() on the Page class.

    Hope this helps,

    Cheers,
    Sean

  • NicolasLeuenberger
    Avatar
    Community Member
    9 Posts

    Re: Extending Page with Image Link to this post

    It's true, I do get an Image field where I can upload an image. But it does not get saved. No error message, nothing. It just doesn't save the image, no matter if I uploaded it or selected it from the ressources list...

  • MULTIDOTS
    Avatar
    Community Member
    16 Posts

    Re: Extending Page with Image Link to this post

    Hello All,

    I am too facing the same problem. I am using SilverStripe-v2.3.2 and I have extended the image field for blog entry and blog holder that is working fine.

    But I am facing few problems with image with Virtual page and Blog entry.

    Here are the two cases.

    Case-1: Works for Virtual Page but not for Blog Entry and Blog Holder

    If I put my custom field - "Image" to $has_one, It works for virtual page but whenever I am trying to select an image for a Blog entry that image is not getting assigned to my Blog entry.

    public static $db = array(
          'PhotoID' => 'Int'
       );

    public static $has_one = array(
          'Photo' => 'Image'
       );

    Case-2: Works for Blog Entry and Blog Holder but not for Virtual Page

    If I put my custom field - "Image" to $has_many, It doesn't work for virtual page but it works well for Blog Entry and Blog holder.

    public static $db = array(
          'PhotoID' => 'Int'
       );

    public static $has_many = array(
          'Photo' => 'Image'
       );

    Can anyone please guide me what is the wrong with has_many and has_one relationship? Please help me out on this issue. Tons of thanks in advance.

    Note : Every time I rebuild DB /db/build?flush=1 whenever I made change.

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