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.

Archive

Our old forums are still available as a read-only archive.

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

How can I determine if a image is set


Reply

5 Posts   1613 Views

Avatar
Tkop

21 July 2007 at 10:14am (Last edited: 21 July 2007 10:15am), 28 Posts

I created an image in the "ItemPage extends Page" static $has_one array
static $has_one = array( 'BannerImg' => 'Image' );
but I want to be able to use a default image if the image
has not been set. How can I do that?
I am trying to create a function in the "ItemPage_Controller extends Page_Controller"
but I can't determine in the function if "BannerImg" is actually
set. I tried using:
isset($this->BannerImg) always returns true
strlen($this->BannerImg) always returns false
$this->BannerImg.length always returns a non-zero value
sizeof($thisBannerImg) always returns zero

Avatar
xmedeko

21 July 2007 at 10:27am (Last edited: 21 July 2007 10:29am), Community Member, 94 Posts

[code php]
if ($this->BannerImgID > 0) {
// image is set
} else {
// image is not set
}

or, you can try in the template, but I am not sure, if it works

<% if BannerImg %>
$BannerImg
<% else %>
<img src="default.png"/>
<% end_if %>

Avatar
Willr

21 July 2007 at 1:27pm Forum Moderator, 5511 Posts

<% if BannerImg %>
$BannerImg
<% else %>
<img src=\"default.png\"/>
<% end_if %>

should work fine btw, we use that method quite alot

Avatar
Tkop

21 July 2007 at 1:50pm 28 Posts

Thanks

Both methods worked. The second method <% if BannerImg %> is what I really
wanted but I guess as a newbee I was trying to mix <% control Level() %> and
getting it wrong.

On the first method, where is it Documented about the BannerImgID? Do
all created Fields have that (ie. <field>ID)?

Avatar
Willr

22 July 2007 at 2:21pm Forum Moderator, 5511 Posts

"On the first method, where is it Documented about the BannerImgID? Do
all created Fields have that (ie. <field>ID)?"

Not all fields, Banner ID does as its a has_one. This page may help - http://doc.silverstripe.com/doku.php?id=datamodel

"Define 1-to-1 joins in $has_one. The LHS is the relationship name, the RHS is the class you're linking to. This will create a field called (relationship-name)ID, e.g., [[ParentID]]." or in your case BannerID