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.

Customising the CMS

Changing value in a column of DataGrid

Go to End

11 Posts   778 Views

George Botley

15 October 2013 at 2:18am Community Member, 16 Posts

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.



15 October 2013 at 5:22am Community Member, 528 Posts

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

George Botley

15 October 2013 at 11:44am Community Member, 16 Posts

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):

Grid 2 (Articles):

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.


15 October 2013 at 12:24pm Community Member, 528 Posts

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

George Botley

16 October 2013 at 11:23am Community Member, 16 Posts

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:

The NewsCategory table:

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?


16 October 2013 at 11:52am Community Member, 528 Posts

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 or another paste service

George Botley

16 October 2013 at 12:26pm Community Member, 16 Posts

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.



16 October 2013 at 1:01pm Community Member, 528 Posts

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