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.

Data Model Questions /

Extending Page with Image


Reply


4 Posts   2120 Views

Avatar
NicolasLeuenberger

Community Member, 10 Posts

20 March 2009 at 4:16am

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

Avatar
Sean

Forum Moderator, 922 Posts

23 March 2009 at 9:28pm

Edited: 23/03/2009 9:36pm

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

Avatar
NicolasLeuenberger

Community Member, 10 Posts

23 March 2009 at 11:21pm

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

Avatar
MULTIDOTS

Community Member, 16 Posts

26 February 2010 at 1:40am

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.