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.

Data Model Questions /

2 related DataObject w/ ModelAdmin for directory


Reply


5 Posts   582 Views

Avatar
Wolfbane

Community Member, 5 Posts

15 November 2011 at 7:21am

Edited: 15/11/2011 7:22am

Hi

I'm new to Silverstripe (though not to other CMS flavors) so if someone could take pity on me that would be great! (Edit, I have been reading over the docs and I am still not making the connection).

I'm trying to build a directory of categories/businesses in Silverstripe which seems to be pretty straightforward using DataObject, however, things are getting tricky because I don't have a set number of categories so I'm trying to use ModelAdmin to manage them both.

My thinking so far has been to create two DataObjects (category & business) and then to relate them to each other:

class Category extends DataObject {
static $db = array(
'Name' => 'Varchar(128)'
);

static $belongs_many_many = array(
'Businesses' => 'RecycleDirectory'
);
}

class RecycleDirectory extends DataObject {

static $db = array(
      'Name' => 'Varchar(255)',
      'Address' => 'Varchar(255)'
);
static $many_many = array(
'Categories' => 'Category'
);

Using ModelAdmin to manage the values:

class DirectoryAdmin extends ModelAdmin {

public static $managed_models = array(
'RecycleDirectory',
'Category'
);
static $url_segment = 'DirectoryAdmin';
static $menu_title = 'Recycling Directory';
}

The two issues I'm having are:

1. In ModelAdmin I'm seeing a tab labeled categories (hurray) but I'm not seeing existing categories show up as already selected. Further, I can only add a category (which gives me a form to create a new one) and visa versa when I look at a category I see a "businesses" tab, but when I add to it I get a form to create a new entry instead of select from existing. (See attached)

2. This one is really bothering me: with the Category being a DataObject, I can't figure out how I would then show each Category on the frontend and populate each one with their associated business entries. I would expect to need to set up some sort of association between the Page->DataObject (or to create the page in the CMS and associate businesses to it)

Can anyone shed some light on this?

Thank you in advance!

Attached Files
Avatar
swaiba

Forum Moderator, 1799 Posts

15 November 2011 at 7:59am

Hi Wolfbane,

1) http://www.silverstripe.org/multiselectfield-module/
2) http://doc.silverstripe.org/sapphire/en/tutorials/2-extending-a-basic-site

Breif, but I hope these help!

Avatar
Wolfbane

Community Member, 5 Posts

15 November 2011 at 9:26am

Still looking through postings and such.. I'm not sure that a 3rd party module is what I really want to be using, however, maybe I should be looking at using a ManyManyComplexTableField to manage my relationship?

As far as the 2nd link.. that doesn't get me closer to creating a page with a URL for each DataObject as that suggests making an actual page and associating the DataObjects with them.. :-/

Avatar
swaiba

Forum Moderator, 1799 Posts

15 November 2011 at 11:07am

Edited: 15/11/2011 11:09am

I'm not sure that a 3rd party module is what I really want to be using, however, maybe I should be looking at using a ManyManyComplexTableField to manage my relationship?

Certainly ManyManyComplexTableField is in fact what ModelAdmin scaffolds for you and you can create a custom one in getCMSFields and set various options to change it's behaviour... however having had the exact same problem 2 years ago and having used the external module to solve it since then - I am sure.

As far as the 2nd link.. that doesn't get me closer to creating a page with a URL for each DataObject as that suggests making an actual page and associating the DataObjects with them.. :-/

you previously said...

I would expect to need to set up some sort of association between the Page->DataObject

i agreed and pointed you to a tutorial that you did just that, in case it isn't obvious a Page is inherited from (and thereby equivalent to) a DataObject. To associate the page with the dataobject is straightforward once you have gone through he tutorials. If you are saying you'd prefer to not associate the page with the data object but rather have a single page that is passed parameters to so a specific dataobject then you might try this... http://archive.ssbits.com/using-silverstripe-url-parameters-to-display-dataobjects-on-a-page

Avatar
Wolfbane

Community Member, 5 Posts

15 November 2011 at 11:29am

You just blew my mind a little bit. :)
I'm going to dig into the module (which scares me coming from Drupal and Magento background) but if you say that it'll do the magic I need..

Regarding the latter, you just blew my mind a little bit more.

I'll report back with any updates but I really, really want to thank you for your time in helping a new guy get started with SS.

Matthew