Jump to:

23375 Posts in 18175 Topics by 2866 members

General Questions

SilverStripe Forums » General Questions » [resolved] issues adding image upload field to cms

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

Page: 1
Go to End
Author Topic: 2373 Views
  • prawnstar
    Avatar
    Community Member
    116 Posts

    [resolved] issues adding image upload field to cms Link to this post

    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 {
    }
    ?>

  • ampedup
    Avatar
    Community Member
    133 Posts

    Re: [resolved] issues adding image upload field to cms Link to this post

    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.

  • prawnstar
    Avatar
    Community Member
    116 Posts

    Re: [resolved] issues adding image upload field to cms Link to this post

    Thanks ampedup!

    Chris.b

    2373 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.