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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Call to undefined method DataObject::image()


Go to End


4 Posts   1960 Views

Avatar
warden

Community Member, 2 Posts

26 January 2010 at 11:02pm

Edited: 26/01/2010 11:03pm

Hi,

i try to output a default image if no image is set:

class myclass extends DataObject {
  static $has_one = array(
      'Image' => 'Image'
   );

  public function Image()
  {
      if($this->ImageID)  return parent::Image(); 
      else return File::find('assets/default.png'); 
  }
}

On my local machine it works fine, but on webserver i get the mesasge
Call to undefined method DataObject::image()

Avatar
baba-papa

Community Member, 279 Posts

27 January 2010 at 4:44am

Edited: 27/01/2010 5:11am

I would realize this via the template:

<% control myclass %>
<% if Image %>
$Image
<% else_if %>
[hardcoded Link]
<% end_if %>
<% end_control %>

With "parent::Image()" you´re infact calling DataObject::Image() which does not exist.

Avatar
baba-papa

Community Member, 279 Posts

27 January 2010 at 5:41am

The template error was due to an error in the nested conditionals in the layout/ChatPage.ss tempate. I changed it and appended it to this post.

Avatar
warden

Community Member, 2 Posts

27 January 2010 at 8:09am

Edited: 27/01/2010 8:09am

I would realize this via the template:

I think this is not really comfortable. Because my posted code is only an example.

In real code there are two default images. One for male and another one for female users.
And the myclass is a user-management class. The Image is called in many Templates. And often with different sizes using $Image.setWidth(xx)

With "parent::Image()" you´re infact calling DataObject::Image() which does not exist.

But I'm wondering why this works on my local machine. (php Version 5.2.10).
But same Code on my Webserver (php Version 5.2.0-8) causes this error.