Search

Howdy all. Total noob here, but I’m definitely liking Symphony.

Part of a learning site I’m building contains a blog, and as is normal with blogs, it has archive sub-navigation that lists years or years and months of existing articles. To get those years I have a data source fetching all the published article dates sorted and grouped by date. Everything works fine, but I feel like I’m not doing this the right way. For blogs with a high post frequency, it seems like I’m having to drag up a lot of data just to get a handful of corresponding years.

For example, say the blogger posts daily for 5 years. To get 5 year entries in the navigation, I’d be fetching 1800+ items. But all I need from the XML side is 5 items, one for each year. Is there a way to perform a query for distinct subsets of the date value (unique years)? I haven’t found anything addressing this in the forum yet. Am I going about this all wrong? Or is this an insignificant efficiency I’m after?

Thanks in advance.

Hey BBQ welcome!

You’re dead right, what you’re after is more than an insignificant efficiency. You need to add additional filters to your datasource. Specifically, it sounds like you would filter by the year. (I’m not sure what your section looks like, but I usually prefer to create my own ‘date’ field rather than use the system date, so I can change it if I need to backdate an entry.) Anyway, assuming that you have a ‘date’ field:

  1. Add a url parameter for year to your Symphony Page (eg ‘year’)
  2. Add a filter to your data source that filters using your date field by the $year parameter

Now, if a user enters ‘example.com/archives/2006’ The ‘2006’ will pass into the Symphony param pool as the value of $year. The DS will then filter its results based on that year. Keep in mind that the default behavior for this would lead the ‘example.com/archives’ page to output all years, unless one is specified. You can read all about parameters here.

Hope that helps & good luck!

But I think BBQbrains wants a single page containing a list of years, without any filtering.

Is there a way to perform a query for distinct subsets of the date value (unique years)?

I don’t think there is. Because Data Sources (and their Grouping) are generic enough to build almost anything, this specific use case can’t be satisfied. The only way I can see this working without any custom code is using a Select Box Link field:

  • Create a new section “Months” with a single text input “Month”
  • Add entries into this field, one for each month (e.g. “January 2005”, right through to “December 2010”)
  • In your blog post section add a Select Box Link field pulling its values from this Month field
  • When you create a blog post entry, you select the month from the drop down

To build your list of years/months you then create a Data Source that selects from the Months section only. Each <entry> element in the XML will have an attribute with the count of blog post entries using that month.

While this works, it’s not particularly elegegant or futureproofed since each month you would need to add an entry into Months manually.

The alternative is to write a custom Data Source (in PHP) that does this. Perhaps this would be a good piece of code to share. The pseudo-code would be along the lines of:

  • receive the section and field name of your Date field
  • select the minimum and maximum values (start/end dates)
  • build a date object in PHP and loop over months between these two dates
  • for each month, query that field’s database table and count the number of rows that match
  • return to XML

Another alternative is to create a separate page that outputs the date menu as XML. You can than use this XML as a Dynamic XML Datasource. This way you automatically have a caching mechanism.

Thanks for the responses.

@ashooner

I hear what you are saying, and I am actually doing what you mentioned. But what I was really after was a way to build a standard navigation piece for all the archived articles. Rather than force the user to click through a large number of paginations to get to an article in year or month x, I would rather provide links to year or year and month, but only if there are actual articles published in those years and months. That means I need to pull up the dates for all existing articles every time I generate the navigation which is on all the assorted blog pages.

@nickdunn

Yeah, I had thought of trying something like your first solution, but it has that Y2K feel. :)

I may have to ultimately go with your latter approach, but since Data Sources are basically queries, it might be useful in the future to provide an equivalent for the DISTINCT command. That way you could select distinct year or distinct year-month values without having to convert a lot of rows into XML only to be thrown away later.

@klaftertief

I hadn’t thought of that approach. It would still be inefficient on the first hit, but less so than what I’ve been doing. However, the content publisher might experience some consternation if their entries don’t show up for them in the navigation until a siginificant amount of time later.

@BBQBrains

Yeah, Nick set me straight on what you were looking for. This is a really good use case that needs to be addressed.

it might be useful in the future to provide an equivalent for the DISTINCT command.

Nick, would something like this have a place in SymQL?

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details