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

SS3 DataExtension + Data Object


Go to End


9 Posts   7171 Views

Avatar
Craftnet

Community Member, 58 Posts

6 July 2012 at 3:04am

Hi,

I have problem.

Generally I want in one place add pictures to the sliders and they are to be displayed on all pages.

In SS 2.4.7 I add to SiteConfig
In SS3 I would do the same so:

I try add to DataExtension DataObject

AllSite.php

class AllSite extends DataExtension {
	
	static $has_many = array (
        'Sliders' => 'Slider'
    );
	
	public function updateCMSFields(FieldList $fields) { 
	  
	  
	  //SLIDER
		$gridFieldConfigSlider = GridFieldConfig::create()->addComponents(
			new GridFieldToolbarHeader(''),
            new GridFieldAddNewButton('toolbar-header-right'),
			new GridFieldDataColumns(),
			new GridFieldEditButton(),
			new GridFieldDeleteAction(''),
			new GridFieldDetailForm()
		);
		
		$gridFieldSlider = new GridField('Sliders', 'Slider', new DataList('Slider'), $gridFieldConfigSlider);
		$fields->addFieldToTab('Root.Slider', $gridFieldSlider);

   } 	
}

class Slider extends DataObject
{
    static $db = array (

    );
 
    static $has_one = array (
        'Slider' => 'Image',
        
    );
	
	function getCMSFields() {
     return new FieldSet(
           new UploadField('Slider')
     );
   }
}

in template i add:

<% loop SiteConfig %>
	$Slider
<% end_loop %>

inn backend i can add image to slider but in frontend in right place picture not show

Who have any idea?

Sorry for my bad English

Avatar
martimiz

Forum Moderator, 1391 Posts

6 July 2012 at 7:24am

Maybe something like:

<% with SiteConfig %>
	<% loop $Sliders %>
		$Slider
 	<% end_loop %>
<% end_with %>

Avatar
Craftnet

Community Member, 58 Posts

6 July 2012 at 8:00am

Edited: 06/07/2012 8:00am

thx for replay.

still nothing,
In you'r set i have errorp (you have to remove the $ in the control)
. I'm not sure if the code is correct, but even that everything looks good :)

If for example I will add to the database
static $ db = array (
         'FooterContent' => 'htmlText'
     );

then <% control SiteConfig%> work but not in my example.

In last resort, write a function that collects in Page_Controller in someone side of these figures but I'd also know such a solution.

Sorry for my bad english

Avatar
martimiz

Forum Moderator, 1391 Posts

6 July 2012 at 8:15pm

Edited: 06/07/2012 8:15pm

Sorry about the & :-(

Does your Slide object link back to SiteConfig, using something like:

static $has_one = array('SiteConfig' => 'SiteConfig');

i've not yet used the GridField from the SiteConfig, so I don't know if it works at all...
You could try a ?showqueries=1 to check if the query on Slides is actually executed and what it looks like...

Avatar
Jaymonkey

Community Member, 10 Posts

25 February 2014 at 11:30am

I don't suppose anybody got to the bottom of this problem?

I am using a data extension to extend the site config. I have a $has_many data object in my extension. I have used a GridField and all the relevant fields are showing in the settings page only when I hit the save button they are not being stored!

My CustomSiteConfig Class:

<?php
class CustomSiteConfig extends DataExtension {

private static $has_many = array(
'Sponsors' => 'Sponsor'
);

public function updateCMSFields(FieldList $fields) {

$config = GridFieldConfig_RelationEditor::create();

$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Title' => 'Title'
));

$sponsorfield = new GridField(
'Sponsors',
'Sponsor',
new DataList('Sponsor'),
$config
);

$fields->addFieldToTab('Root.Main', $sponsorfield);
}

}

?>

My DataObject Class:

<?php
class Sponsor extends DataObject {

static $db = array (
'title' => 'text'
);

static $has_one = array (
'CustomSiteConfig' => 'CustomSiteConfig',
'Image' => 'Image',
'Link' => 'SiteTree'
);

public function getCMSFields() {
$sizeMB = 2; // 2 MB
$size = $sizeMB * 1024 * 1024; // 2 MB in bytes

$uploadField = new UploadField('SponsorImage', 'Sponsor Image');

$uploadField->getValidator()->setAllowedMaxFileSize($size);

$fields = new FieldList(
new TextField('Title'),
$uploadField,
new TreeDropdownField('LinkID', 'Link', 'SiteTree')
);

return $fields;
}
}

?>

Can anyone help please?

Avatar
Craftnet

Community Member, 58 Posts

25 February 2014 at 12:39pm

Edited: 25/02/2014 12:40pm

Do somthing like this:
Of course this is example!

<?php
class Galeria extends DataObject {

    private static $db = array(
        'Title' => 'Varchar'
    );

    private static $has_one = array(
        'Image' => 'Image',
        'GalleryPage' => 'GalleryPage'
    );

    private static $searchable_fields = array(
        'Title' => 'Tytuł'
    );

    public function getCMSFields() {
        $fields = new FieldList();
        $fields->push( $type = new UploadField("Image", "Image"));
        $type->allowedExtensions = array('jpg', 'gif', 'png');
		$type->setFolderName('Galeria');
        $fields->push( new TextField("Title", "Krótki opis"));

        return $fields;
    }

    // Tell the datagrid what fields to show in the table
    public static $summary_fields = array(
        'Title' => 'Tytuł',
        'Thumbnail' => 'Thumbnail'
    );

    public function getThumbnail() {
        return $this->Image()->CMSThumbnail();
    }

}

<?php
class GalleryPage extends SiteTree {

    private static $db = array(
        //'Title' => 'Varchar'
    );

    private static  $has_one = array(
    
    );

    private static $has_many = array(
        'Gallerys' => 'Gallery',
    );

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

        $gridFieldConfig = GridFieldConfig_RecordEditor::create();
        $gridfield = new GridField("Gallerys", "Galeria", $this->Gallerys(), $gridFieldConfig);
        $fields->addFieldToTab('Root.Galeria', $gridfield);

        /*
        $fields->addFieldsToTab('Root.Top', array(
        	new TextField('TitleTop', 'Tytuł')
        ));
		*/

        return $fields;
    }

}

class GalleryPage_Controller extends Page_Controller {

}

IMPORTANT
in Page.php in Controller
public function GallerysOnAllSite() { return GalleryPage::get(); }

then in template

<% loop GallerysOnAllSite%>
<% loop Gallery %>
...
<% end_loop %>
<% end_loop %>

Sorry for my bad English

Avatar
Jaymonkey

Community Member, 10 Posts

25 February 2014 at 9:26pm

So it's not possible to do this by extending site config?

Avatar
martimiz

Forum Moderator, 1391 Posts

25 February 2014 at 10:49pm

Edited: 25/02/2014 10:56pm

i would think your Sponsor's has_one should point to the SiteConfig class, not the CustomSiteConfig, as the latter is not a DataObject in itself, it.just enhances the SiteConfig...

Edit: An alternative would be to just use ModelAdmin for your Sponsors and in your Page_Controller class create a function like this:

public function getAllSponsors() {
return Sponsor::get();
}

Then <% loop $AllSponsors %> in your Page template...

Go to Top