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

Required Fields in CMS - has_one, has_many


Reply

22 Posts   4530 Views

Avatar
ttyl

9 July 2010 at 4:57am Community Member, 114 Posts

"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

Avatar
swaiba

9 July 2010 at 5:00am Forum Moderator, 1796 Posts

How about sharing your code?

Avatar
ttyl

9 July 2010 at 5:05am (Last edited: 9 July 2010 5:07am), Community Member, 114 Posts

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

Avatar
swaiba

9 July 2010 at 5:13am Forum Moderator, 1796 Posts

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 :)

Avatar
ttyl

9 July 2010 at 5:22am (Last edited: 9 July 2010 5:27am), Community Member, 114 Posts

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!)

Avatar
swaiba

9 July 2010 at 5:32am Forum Moderator, 1796 Posts

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

Avatar
ttyl

9 July 2010 at 5:43am Community Member, 114 Posts

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)

Avatar
swaiba

9 July 2010 at 5:45am Forum Moderator, 1796 Posts

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