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.

Template Questions /

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

ImageField / Image Variable Problems (only show if something is uploaded)

Go to End

12 Posts   1899 Views


Community Member, 72 Posts

11 July 2011 at 4:08am

(pls excuse my probably bad english and my small knowledge about ss and php i'm totaly new to this)

Hi everyone,

i think the title isn't very significantly so i'll tell my problem here more detailed.

at first, here's my mysite/code/Referenz.php

class Referenz extends Page {
    static $db = array(
    	'Description' => 'Text',
    	'Kunde' => 'Text',
    	'Website' => 'Text',
    	'Leistung_1' => 'Text',
    	'Leistung_2' => 'Text',
    	'Leistung_3' => 'Text',
    	'Leistung_4' => 'Text',
    	'Leistung_5' => 'Text',
    	'Leistung_6' => 'Text',
    	'Leistung_7' => 'Text',
    	'Leistung_8' => 'Text',
    	'Leistung_9' => 'Text',
    	'Leistung_10' => 'Text'
    static $has_one = array(
        'Preview' => 'Image',
        'RefImg_1' => 'Image',
        'RefImg_2' => 'Image',
        'RefImg_3' => 'Image',
        'RefImg_4' => 'Image',
        'RefImg_5' => 'Image',
        'RefImg_6' => 'Image',
        'RefImg_7' => 'Image',
        'RefImg_8' => 'Image',
        'RefImg_9' => 'Image',
        'RefImg_10' => 'Image'
    function getCMSFields() {
        $fields = parent::getCMSFields();
     	$fields->addFieldToTab('Root.Content.Main', new TextField('Description','beschreibung der Seite'), 'Content');
	  	$fields->addFieldToTab("Root.Content.Referenz", new ImageField('Preview'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_1'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_2'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_3'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_4'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_5'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_6'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_7'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_8'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_9'));
		$fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_10'));
	  	$fields->addFieldToTab('Root.Content.Main', new TextField('Kunde','Kunde des Projekts'));
		$fields->addFieldToTab('Root.Content.Main', new TextField('Website','Website / Live Preview (mit http://)'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_1','erbrachte Leistung 1'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_2','erbrachte Leistung 2'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_3','erbrachte Leistung 3'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_4','erbrachte Leistung 4'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_5','erbrachte Leistung 5'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_6','erbrachte Leistung 6'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_7','erbrachte Leistung 7'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_8','erbrachte Leistung 8'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_9','erbrachte Leistung 9'));
		$fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_10','erbrachte Leistung 10'));
        return $fields;
class Referenz_Controller extends Page_Controller {

The "RefImg_x" variables are all listed in the at the same time. But e.g when i uploaded images only to RefImg_1 and RefImg_2, RefImg_3 - 10 will show the folder path "assets/" -.- how can i stop that? so that the variable only shows something if there's an image uploaded for it?

is there a function which hides the variables where's no image uploaded? which one?

pls help me :)


Community Member, 541 Posts

15 July 2011 at 7:19am

You may be better off storing your images in a separate DataObject using the DataObjectManger Module instead of trying to force it into your Page Object. SO That way each instance of a RefImage could keep its own set of variable.

So your page would have

static $db = array( 
   'Description' => 'Text', 
   'Kunde' => 'Text', 
   'Website' => 'Text'

static $has_many = array( 
   'RefImages' => 'RefImage'

and then you'd have a RefImage DataObject

class RefImage extends DataObject {
static $db = array( 
   'Leistung' => 'Text'

static $has_one = array(
'RefImg' => 'Image'

Refer to the DataObjectManager Documentation for how to implement it in the CMS


Community Member, 72 Posts

16 July 2011 at 12:02am

i'll try it later an post the result :)


Community Member, 72 Posts

16 July 2011 at 12:52am

Damn :/

trying this: doesn't work, if i copy paste the code, the only result is an blank little popup which shows nothing.

Cause of that i tried this one
i edit the code to fit my requirements like this


then i did an dev/build?flush=1 and created an Referenz Site on the Admincenter => Error -.-

What did i wrong?
I don't understand the whole thing :/


Community Member, 541 Posts

16 July 2011 at 1:34am

I think you misunderstanding the concept of the relations work. Page should have the general information stored in its $db with a $has_many relation to Refrenzen. $has_one and $has_may represent relationships between data objects. It should be structured like this.

Your Page Object should have the general information the page plus a has_many relation with your Refrenzen DataObject. Since Refrenzen Is really mainly holding an image you can you an ImageDataObjectManager

Then your Refrenzen object only needs to hold Image and Leistung

You need to think of Refrenzen as Children of Referenz as opposed to be being fields on it

Hope this makes sense


Community Member, 72 Posts

16 July 2011 at 2:49am

I don't know how to explain that in english, so i drawed an image ;)

So Referenz should be Children of Referenzen

Do you know what i mean with that picture?

I tryed to build that with the dataobject, but theres always an error :/

Attached Files

Community Member, 541 Posts

19 July 2011 at 1:38am

Okay are Refrenzen and Refrenz both Pages? or is Refrenz just some data stored on a Refrenzen Page. Are you getting an error when building the Database or when Trying to access it in the CMS


Community Member, 72 Posts

19 July 2011 at 9:11am

yes Referenzen is one Page and on this page are many links + preview images of the difference referenz. if you click on one of them, you'll be redirected to a page with the type Referenz.

In english you can say that a referenz is a project you designed (for example) and referenzen is the plural of that.

The error comes while building a referenz page in the cms.
silverstripe says server error and theres a blank silverstripe popup

Go to Top