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.

Customising the CMS /

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

Changing value in a column of DataGrid


Go to End


11 Posts   2916 Views

Avatar
George Botley

Community Member, 18 Posts

15 October 2013 at 2:18am

Hi Guys,

I have a news page type that makes use of two gridfield elements.

One is Categories, the other is Articles.

The Categories is working fine in the sense that you can add remove etc.. No problems here.

The Articles additions works fine also, it's just the displaying of the Articles GridField that would otherwise confuse users.

When an article is created, a user is given a dropdown to select the assigned category, which saves the ID of the Category from the DB into the record for the article, which allows the category name to be changed in the future without affecting articles assigned to it...

The confusing part is when the user then views all of their Articles, they can see the article name, and the assigned category comes up as the ID.. How can I make that particular column fetch its value based on the actual category name within the Categories table as opposed to the ID? - It would involve a SQL query, which is fine, but I don't know how to alter the values in the displayed output of the Grid.

Much obliged.

George

Avatar
zenmonkey

Community Member, 545 Posts

15 October 2013 at 5:22am

If you're using a GridField you need ot set $summary_fields on Categories or custom specify the header fields in gridfield. Gridfield uses $summary_fields for scaffolding geader by default

Avatar
George Botley

Community Member, 18 Posts

15 October 2013 at 11:44am

Thanks ZenMonkey, but I didn't quite see what you meant..

I've set that already...

I'll give you the two grids as examples...

Grid 1 (Categories): http://screencast.com/t/mw99fdz5QPQw

Grid 2 (Articles): http://screencast.com/t/gMC77eyHeUK

How do I get the Grid of Articles to show the Category Name from the associated record in the Categories grid...

Here's the summary fields of each respective grid as you mentioned..

Grid 1:

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

Grid 2:

  // Tell the datagrid what fields to show in the table
   public static $summary_fields = array('ID' => 'ID', 'Title' => 'Title', "Category" => "Cateogry");

Much obliged.

Avatar
zenmonkey

Community Member, 545 Posts

15 October 2013 at 12:24pm

Okay, now I have a better idea of what's going on. You need to set that column with dot notation so instead of Category you'd use Category.Title

Avatar
George Botley

Community Member, 18 Posts

16 October 2013 at 11:23am

Thanks zenmonkey,

But that still didn't quite work..

Here's a structure screenshot of my database tables for each grid field..

The NewsArticle table: http://screencast.com/t/tS24TtTcm2Yk

The NewsCategory table: http://screencast.com/t/r6L4ebGQH

I'm trying to link two has_many grid fields together..

So that the NewsArticle with Category '1' would refer to the name of the category based on the same respective ID in the NewsCategory table...

Any pointers? - Or am i approaching this in the completely wrong way?

Avatar
zenmonkey

Community Member, 545 Posts

16 October 2013 at 11:52am

Are you manually building the relationation intead of letting SS do it? If you're leeting the ORM build the realtion the column should be called CategoryID. Can you post your code for NewsArticle and NewsCategory?

you can use http://www.sspaste.com or another paste service

Avatar
George Botley

Community Member, 18 Posts

16 October 2013 at 12:26pm

I'm not sure.. I'm quite new to using SS GridFields so I'm not sure how to do either, although I've worked with SS for a few years.

Article: http://www.sspaste.com/paste/show/525dcd54a06b9
Category: http://sspaste.com/paste/show/525dcd6f83886

Avatar
zenmonkey

Community Member, 545 Posts

16 October 2013 at 1:01pm

Okay, I'm not sure why you're setting the Category manually instead of setting it as a relationship. If you set up NewsCategory $has_many NewArticles and NewsArticle $has_one NewsCategory then all the properties from the respecitve obejct will be availble on the other Object. I've posted the cahnges on the respective sspaste links

If you're storing the Category on the article to save an extra Database Call for the relation don't map('ID', "Title'). You should map('Title', 'Title') on the dropdown.

map('ID', 'Title') set the selct array as 'ID' => 'Title', the key get written the database while the value shows in the drop down.

Go to Top