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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

trying to adapt ressource example to modeladmin


Go to End
Reply


11 Posts   1460 Views

Avatar
servalman

Community Member, 211 Posts

16 June 2011 at 4:42am

Edited: 16/06/2011 4:44am

Hello

I'm trying to do the following using model admin :

I have categories who have sub categorie (works) and each works has several file (images) attached to them so it looks like this.

CATEGORY 1
Work 1 > multiple file attachment
Work 2 > multiple file attachment

To do so I used the example given in the modeladmin wich gives me :

WorkManager.php

<?php
class WorkManager extends ModelAdmin {

public static $managed_models = array(
'Work',
'Category'
);

static $url_segment = 'works';
static $menu_title = 'WorksManager';

}

Work.php

<?php
class Work extends DataObject {

static $db = array(
'Name' => 'Varchar'
);

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

static $searchable_fields = array(
'Name'
);

}

Category.php

<?php
class Category extends DataObject {
static $db = array(
'Title' => 'Text'
);
}
?>

this part works fine and I understand how it works.

Then I get lost, I had set up in previous project a Ressource page based on some other example :

Ressource.php

<?php
class Resource extends DataObject
{
   static $db = array (
      'Name' => 'Text',
   );
   
   static $has_one = array (
      'Attachment' => 'File',
      'ResourcePage' => 'ResourcePage'
   );
   
   public function getCMSFields_forPopup()
   {
      return new FieldSet(
         new TextField('Name'),
         new FileIFrameField('Attachment')
      );
   }
}
?>

RessourcePage.php

<?php
class ResourcePage extends Page
{
   static $has_many = array (
      'Resources' => 'Resource'
   );
   
   public function getCMSFields()
   {
      $f = parent::getCMSFields();
      $manager = new FileDataObjectManager(
         $this, /
         'Resources',
         'Resource',
         'Attachment',
         array(
            'Name' => 'Name',
            'Description' => 'Description'
         ),
         'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
   
      );
      
      
            
      $f->addFieldToTab("Root.Content.Resources", $manager);

      return $f;
   }

}
class ResourcePage_Controller extends Page_Controller {

}

}

That also was working fine

Could anybody show me how to combine those 2 approach.

Right now I only want to load datas, my next step will be to create a CategoryPage but I'm trying to do this step by step with a simple example.

Thanks for any help

T

Avatar
UncleCheese

Forum Moderator, 4094 Posts

16 June 2011 at 5:53am

I'm really confused about what you're trying to do. How to the categories and ResourcePages relate?

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
servalman

Community Member, 211 Posts

16 June 2011 at 6:14am

Edited: 16/06/2011 7:34am

Thank for your answer !

The ressource example was something I used in previous exersise .

Now I trying to use model admin to deal with datas .

So I would like to have my Work dataobject to behave like a ressource that hold several uploaded files.

Is it clearer ?

I'm very new to this maybe I'm doing it the wrong way

T

Avatar
UncleCheese

Forum Moderator, 4094 Posts

16 June 2011 at 8:45am

class Work extends DataObject {

static $db = array(
'Name' => 'Varchar'
);

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

static $has_many = array (
'UploadedFiles' => 'WorkFile'
);

static $searchable_fields = array(
'Name'
);

public function getCMSFields() {
return new FieldSet (
new TextField('Name'),
new MultipleFileUploadField('UploadedFiles','Files')
);
}

WorkFile.php

class WorkFile extends File {
static $has_one = array (
'Work' => 'Work'
);
}

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
servalman

Community Member, 211 Posts

16 June 2011 at 8:53am

Thank you Uncle Fromage

If you have a bit of time can you tell me if this is the right approach for that type of project.
I'm getting lost because the more I read things on the forum the more I don't know if it is best to use DOM or modeladmin or both

In the exemple I'm trying to set up with the modeladmin will the next step (not asking you to do it for me of course;) be :

Creating a CategoryPage and the template that goes allong ;
It will display all the categories, their attached works and the images attached to those works

Is it the right way of doing it ?

Thank you

Avatar
servalman

Community Member, 211 Posts

16 June 2011 at 7:50pm

Edited: 16/06/2011 8:29pm

Hello Uncle Cheese

I tried to set up your solution but I have now 2 problems and 2 question :

First the questions :

1) The work file you created is it there to create the relationship between the work and the attached files ?

2) Is it possible to add this uploadfilds in an tab like the tab I get for ressource in my ressource page example ?
I guess it would have something to do with addFieldToTab function

Now my 2 errors :
I have lost the dropdown to select a categorie in the work tab :
Do I have to add a specific fieldd in the fieldset ?

public function getCMSFields() {
return new FieldSet (
new TextField('Name'),
new MultipleFileUploadField('UploadedFiles','Files')
);

The other problem is that once I save the work I have added all the images the little thumbnails turns white this problem is related to my questio about gettin a specific tab for the images attached to the work.
In fact I noticed taht once uploaded ans saved the image seems to disppear even tough they exist in the upload folder i get :
No files attached under the upload field

Hope I'm not to demanding

Thank you

Avatar
UncleCheese

Forum Moderator, 4094 Posts

17 June 2011 at 5:28am

The code I gave you was just an example. You can set up your FieldSet any way you want, with whatever tabs and fields you need.

$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Files", new MultipleFileUploadField(....

etc..

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
servalman

Community Member, 211 Posts

17 June 2011 at 5:36am

Hello Uncle cheese

I tried to add this : new DropdownField('Category'),

in

public function getCMSFields() {
return new FieldSet (
new TextField('Name'),
new DropdownField('Category'),
new MultipleFileUploadField('UploadedFiles','Files')
);
}

But I cant get the caterogry dropdown to show me the categories.

The weird thing is that if I remove the public function getCMSFields() {} block
I can see them again in a drop down menu

I'm a bit lost I don't understand why this block makes them disapear.

Thanks

Go to Top