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.

General Questions /

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

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

Required Fields in CMS - has_one, has_many


Go to End


22 Posts   7607 Views

Avatar
ttyl

Community Member, 114 Posts

9 July 2010 at 4:57am

"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

Forum Moderator, 1899 Posts

9 July 2010 at 5:00am

How about sharing your code?

Avatar
ttyl

Community Member, 114 Posts

9 July 2010 at 5:05am

Edited: 09/07/2010 5:07am

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

Forum Moderator, 1899 Posts

9 July 2010 at 5:13am

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

Community Member, 114 Posts

9 July 2010 at 5:22am

Edited: 09/07/2010 5:27am

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

Forum Moderator, 1899 Posts

9 July 2010 at 5:32am

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

Community Member, 114 Posts

9 July 2010 at 5:43am

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

Forum Moderator, 1899 Posts

9 July 2010 at 5:45am

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