Jump to:

23355 Posts in 17734 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Required Fields in CMS - has_one, has_many

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 2 3
Go to End
Author Topic: 4340 Views
  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    "ImageID" doesn't work, I used php logging with this code

             foreach ($data as $v => $d){
                error_log($v.' >> '.$d, 0);
             }
             
             error_log('Image ID is '.$data['Image']->ID, 0);

    and got this result where I don't see ImageID

    [08-Jul-2010 12:55:16] Title >> New EventPage
    [08-Jul-2010 12:55:16] MenuTitle >> New EventPage
    [08-Jul-2010 12:55:16] Date >> 2010-07-17
    [08-Jul-2010 12:55:16] Time >>
    [08-Jul-2010 12:55:16] Location >>
    [08-Jul-2010 12:55:16] Image >>
    [08-Jul-2010 12:55:16] Content >>
    [08-Jul-2010 12:55:16] URLSegment >> new-eventpage-3
    [08-Jul-2010 12:55:16] MetaTitle >>
    [08-Jul-2010 12:55:16] MetaKeywords >>
    [08-Jul-2010 12:55:16] MetaDescription >>
    [08-Jul-2010 12:55:16] ExtraMeta >>
    [08-Jul-2010 12:55:16] Priority >> 0.9
    [08-Jul-2010 12:55:16] Units >> Array
    [08-Jul-2010 12:55:16] ClassName >> EventPage
    [08-Jul-2010 12:55:16] ParentType >> subpage
    [08-Jul-2010 12:55:16] ParentID >> 24
    [08-Jul-2010 12:55:16] ShowInMenus >> 1
    [08-Jul-2010 12:55:16] ShowInSearch >> 1
    [08-Jul-2010 12:55:16] ProvideComments >> 0
    [08-Jul-2010 12:55:16] HomepageForDomain >>
    [08-Jul-2010 12:55:16] ToDo >>
    [08-Jul-2010 12:55:16] CanViewType >> Inherit
    [08-Jul-2010 12:55:16] ViewerGroups >>
    [08-Jul-2010 12:55:16] CanEditType >> Inherit
    [08-Jul-2010 12:55:16] EditorGroups >>
    [08-Jul-2010 12:55:16] ID >> 116
    [08-Jul-2010 12:55:16] LiveURLSegment >> http://localhost:8888/events/new-eventpage-3/
    [08-Jul-2010 12:55:16] StageURLSegment >> http://localhost:8888/events/new-eventpage-3/
    [08-Jul-2010 12:55:16] Sort >> 14
    [08-Jul-2010 12:55:16] Image ID is

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    How about sharing your code?

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    also, even if I add an image and save and then save again I still don't get my image id.

    my code is as follows:

    <?php
    class MyValidator extends Validator{
       function javascript(){
          return false;
       }

       function php($data){
             foreach ($data as $v => $d){
                error_log($v.' >> '.$d, 0);
             }
             
             error_log('Image ID is '.$data['Image']->ID, 0);
             
          $bRet = true;

          //do some validation on the data
          /*
          if (!$data['Image']){
             $this->validationError(
                'Image',
                'We need an image',
                "required"
             );

             $bRet = false;
          }
    */
          return $bRet;
       }
    }
    ?>

    and

    <?php
    /**
    * Defines the EventPage page type
    */
    class EventPage extends Page {
       static $db = array(
          'Date' => 'Date',
          'Time' => 'Text',
          'Location' => 'Text',
       );
       
       static $has_one = array(
          'Image' => 'Image',
       );

       static $many_many = array (
          'Units' => 'Unit',
       );

       function getCMSValidator(){
          return new MyValidator();
       }

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

          $fields->addFieldToTab('Root.Content.Main', new DatePickerField('Date','Date'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('Time', 'Time'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('Location', 'Location'), 'Content');

          $fields->addFieldToTab('Root.Content.Main', new ImageField('Image', 'Image', '', '', '', $folderName = 'Events/'.substr($this->Date, 0, 7)), 'Content');

          $unitsMMCTF = new ManyManyComplexTableField(
             $this,
             'Units',
             'Unit',
             array('Name' => 'Unit Name')
          );

          // we don't want people adding new units!
          $unitsMMCTF->setPermissions(array());

          $fields->addFieldToTab('Root.Content.Units', $unitsMMCTF);
          
          return $fields;
       }

    }

    class EventPage_Controller extends Page_Controller {}
    ?>

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    My first observation is that

    $fields->addFieldToTab('Root.Content.Main', new ImageField('Image', 'Image', '', '', '', $folderName = 'Events/'.substr($this->Date, 0, 7)), 'Content');

    I think should be...

    blahblah ImageField('ImageID', blahblah

    if you look at this in the DB that is how SS actually stores has one => 'Image'. You can clearly see your Image field in your debug results... dataobjects (and $data) can have anything in it but only those items that match the DB will be saved

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    That doesn't make a difference, it just says where to save the file under 'Assets' - if I get rid of it I still don't get anything from $data['Image']. In my DB the EventPage table does have ImageID - but I'm not seeing this under '$data'.

    (thanks for your help and patience!)

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    did you change...

    $fields->addFieldToTab('Root.Content.Main', new ImageField('Image', 'Image', '', '', '', $folderName = 'Events/'.substr($this->Date, 0, 7)), 'Content');

    to

    $fields->addFieldToTab('Root.Content.Main', new ImageField('ImageID', 'Image', '', '', '', $folderName = 'Events/'.substr($this->Date, 0, 7)), 'Content');

    ??

    (Maybe I just should have pasted that, but I was trying to highlight the change...)

    1)Take the line out and you will have it auto generate the field... then look at the data...
    2)Understand that the first param to a FormField (like ImageField) is the name of the item in the DB

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    Well, the image is just named 'Image' so if I change that line to 'ImageID' I get

    [08-Jul-2010 01:40:50] Error at sapphire/core/Object.php line 724: Uncaught Exception: Object->__call(): the method 'imageid' does not exist on 'EventPage' (http://localhost:8888/admin/EditForm/field/ImageID/iframe)

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Required Fields in CMS - has_one, has_many Link to this post

    What does it do for (1) in my last post?

    4340 Views
Page: 1 2 3
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.