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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[resolved] issues adding image upload field to cms


Reply


3 Posts   2531 Views

Avatar
prawnstar

Community Member, 127 Posts

28 June 2010 at 12:04pm

Edited: 28/06/2010 12:28pm

See EDIT below:

Hello and thanks in advance for any help.

Using this tutorial:
http://doc.silverstripe.org/tutorial:2-extending-a-basic-site

I'm trying to add an image and a text field to my page controller, but to no avail.

Here is my code:

<?php
class CustomerQuotesPage extends Page {
   static $db = array(
      'Quotes_Author' => 'Text',
      'Quotes_Image' => 'Image'
   );

   function getCMSFields() {
      $fields = parent::getCMSFields();

      $fields->addFieldToTab("Root.Content.Main", new TextField('Quotes_Author'), 'Content');
      $fields->addFieldToTab("Root.Content.Images", new ImageField('Quotes_Image'));

      return $fields;
   }
}
class CustomerQuotesPage_Controller extends Page_Controller {

}
?>

This is the error I get after i /dev/build/


# Table CustomerQuotesPage: created
# Field CustomerQuotesPage.ID: created as int(11) not null auto_increment
# Field CustomerQuotesPage.Quotes_Author: created as mediumtext character set utf8 collate utf8_general_ci
Website Error
There has been an error

The website server has not been able to respond to your request.

This isn't the first time I've created a cms controller, but it's the first time I've tried to add an image field. When I remove the image field, everything works fine.

Any help is greatly appreciated.

Thanks!
Chris.b

EDIT:

Looks like I didn't follow the tutorial 100%. Note the image needs to use $has_one instead of $db.

Code should have looked like this:

<?php
class CustomerQuotesPage extends Page {
   static $db = array(
      'Quotes_Author' => 'Text'
   );
   static $has_one = array(
      'Quotes_Image' => 'Image'
   );
   function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab("Root.Content.Main", new TextField('Quotes_Author'), 'Content');
      $fields->addFieldToTab("Root.Content.Images", new ImageField('Quotes_Image'));
      return $fields;
   }
}
class CustomerQuotesPage_Controller extends Page_Controller {
}
?>

Avatar
3dgoo

Community Member, 133 Posts

28 June 2010 at 12:27pm

Images need to be in the $has_one array, not in the $db:

public static $db = array(
'Quotes_Author' => 'Text'
);

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

I think this is because an image is not just another field or variable in the database, but a whole object that needs to be pointed to.

Everything else looks good in your code.

Avatar
prawnstar

Community Member, 127 Posts

28 June 2010 at 12:30pm

Thanks ampedup!

Chris.b