8 users online. Create an account or sign in to join them.Users
One datasouce reading from multiple sections
This is an open discussion with 5 replies, filed under General.
Search
Here is something I’m using in 3.0 but it should work in 2.1 after some minor changes - like changing the table nomenclature:
$result = new XMLDocument;
$result->appendChild($result->createElement('category-archive'));
$archive = $result->documentElement;
$query = "SELECT COUNT(category.relation_id) AS count, category.relation_id
FROM sym_data_articles_category AS category, sym_data_articles_publish AS publish
WHERE category.entry_id = publish.entry_id AND publish.value='yes'
GROUP BY category.relation_id
ORDER BY count DESC";
$data = mysql_query($query) or die('Error 1 in category-archive.php: ' . mysql_error());
while ($row = mysql_fetch_assoc($data)) {
$query = "SELECT entry_id,handle,value FROM sym_data_categories_title
WHERE entry_id={$row['relation_id']} LIMIT 1";
$cats = mysql_query($query) or die('Error 2 in category-archive.php: ' . mysql_error());
$cat = mysql_fetch_assoc($cats);
$elmt = $result->createElement('category', $cat['value']);
$elmt->setAttribute('id', $cat['entry_id']);
$elmt->setAttribute('count', $row['count']);
$elmt->setAttribute('handle', $cat['handle']);
$archive->appendChild($elmt);
}
return $result;
In 2.1 you would replace the code in the data source grab function with your code.
So: has anyone ever written a custom datasouce to read data from multiple sections? That would really make my day!
Yes… but because it’s custom it wouldn’t be very useful to you. The custom SQL depends completely on what you’re trying to do and how your sections are structured.
The reason Symphony doesn’t allow you to query from more than one section is that each section’s structure is usually different. So how should Symphony allow you to query from two or more sections at the same time, while also filtering and sorting by fields and values that don’t exist in all of those sections?
If you want to combine two sections together then you can UNION them in SQL. But whatever you do it’s a pretty trivial case of JOINing the tables for each field together and adding a WHERE.
Can you be more specific about what you want to achieve?
Nick, it could be useful to him as an example. That’s what I offered, an example. Of course he will have to modify it to suit his purpose.
Sorry for the late response, your reaction was in fact very useful, wisolman!
I was in quite a bit of stress, so I forgot to think ;) It worked perfectly, so thanks to you both.
My example using SymQuery selects from three sections and sorts the resulting elements by date before attaching them to the XML.
Create an account or sign in to comment.
It’s currently impossible to read from multiple sections with one datasource without hacking some ds code.
I’ve looked at the ds code, and adapting it to supporting multiple sections seems like quite a bit of work.
So: has anyone ever written a custom datasouce to read data from multiple sections? That would really make my day!