UPDATE: replace subclassing with a DataExtension, add has_many warning

I replaced the subclassing example by one using a DataExtension. This
is the preferred way to add extra functionality to the Image class. You
can now add any existing image from the assets section instead of only
those belonging to the subclass.

Added a warning not to use has_many relations, because the UploadField
doesn't fully support them.
This commit is contained in:
martimiz 2012-10-22 12:28:18 +02:00
parent 38b0c672ec
commit 3b65b38826

View File

@ -74,6 +74,8 @@ UploadField will detect the relation based on its $name property value:
class GalleryPage_Controller extends Page_Controller {
}
WARNING: Currently the UploadField doesn't fully support has_many relations, so use a many_many relation instead!
## Set a custom folder
This example will save all uploads in the `/assets/customfolder/` folder. If
@ -132,34 +134,37 @@ selected files that you can then upload manually one by one:
A gallery most times needs more then simple images. You might want to add a
description, or maybe some settings to define a transition effect for each slide.
First create an extended Image class:
First create a
[DataExtension](http://doc.silverstripe.org/framework/en/reference/dataextension)
like this:
:::php
class GalleryItem extends Image {
class GalleryImage extends DataExtension {
static $db = array(
'Description' => 'Varchar(255)'
'Description' => 'Text'
);
public static $belongs_many_many = array(
'GalleryPage' => 'GalleryPage'
);
}
Now simply change the GalleryPage to use the new class:
Now register the DataExtension for the Image class in your _config.php:
:::php
class GalleryPage extends Page {
static $many_many = array(
'GalleryImages' => 'GalleryItem'
);
Object::add_extension('Image', 'GalleryImage');
NOTE: although you can subclass the Image class instead of using a DataExtension, this is not advisable. For instance: when using a subclass, the 'From files' button will only return files that were uploaded for that subclass, it won't recognize any other images!
### Edit uploaded images
By default the UploadField will let you edit the following fields: *Title,
Filename, Owner and Folder*. The `fileEditFields` configuration setting allows
you you alter these settings. One way to go about this is create a
`getCustomFields` function in your GalleryItem object like this:
`getCustomFields` function in your GalleryImage object like this:
:::php
class GalleryItem extends Image {
class GalleryImage extends DataExtension {
...
function getCustomFields() {