I promised more ExpressionEngine tips, and I intend to deliver.
Today, let's take a look at EE's SQL query module. Like the gallery module, this one's only available with a paid license, and for my money, the functionality these two tools bring to the table more than make up for the $100 personal site fee.
The query module is, for lack of a better phrase, almost completely frill-free. It takes one main argument: a valid SQL query statement. It then loops through the results just like exp:weblog:entries, allowing you to access these fields by referencing a field's short name wrapped in curly braces. But where exp:weblog:entries decides what SQL to run based on the parameters you pass it, exp:sql gives you a main-line connection to your database.
True, for most everyday EE tasks, the complex, behind-the-scenes work baked into most tag pairs is a help and not a hindrance – especially if you don't know much (or any) SQL. But in certain circumstances, exp:query is indispensible.
Nesting queries.
One of EE's big limitations is you can't nest one exp:weblog:entries call in another. I recently needed to do just that for a client project. This client is an umbrella organization that oversees around 40 smaller organizations located across the country. In the parent org's parlance, these are each projects. One of the umbrella organizations biggest requests was that each child organization should be able to add and edit their own information, but they should not have access to other organizations' content. Using EE's highly flexible user group system, I set up a separate member group with accounts for each child organization. To get things started, I created one post in a "Projects" weblog per child organization and assigned the associated member as the author. Each entry would then be displayed on a separate page at /projects/organization_name. Because I set the permissions for this member group such that each member has access to only their material, on login to the Control Panel, each org would see only their main page in the edit tab.
But then I ran into a problem. Each org also needed to be able to post news items to the /news/ section with headline links printed to their main project page. Related entries were out of the question, because that would require me to present the child organizations with a drop down of all other child organizations, and leaving open the possibility that they could inadvertently associate their news item with the wrong organization.
I realized that the one thing the project and news items would have in common on each project page was their author. I would simply run a query on the News weblog nested within the main exp:weblog:entries call, using the author id from the former in the latter. (Phew!) What I wanted was this:
- {exp:weblog:entries weblog="projects" url_title="{segment_2}"}
- ...project body...
- {exp:weblog:entries weblog="news" author_id="{author_id}" limit="5"}
- ...news items...
- {/exp:weblog:entries}
- {/exp:weblog:entries}
But as noted, you can't nest calls to exp:weblog:entries.
exp:query to the rescue.
What you can do is nest exp:query inside exp:weblog:entries. You'll run into some variable collision (that is, EE will get confused about which "title" you mean – the project title or the news title – but that can be fixed by either aliasing the variable names or by embedding the exp:query call in a sub-template and passing it an embed variable with the correct author id. I chose the latter. So I ended up with this:
- {exp:weblog:entries weblog="projects" url_title="{segment_2}"}
- ...project body...
- {embed="includes/news_headlines" author_id="{author_id}"}
- {/exp:weblog:entries}
The embedded template contained this:
- {exp:query sql="SELECT title,url_title,entry_date FROM exp_weblog_titles WHERE weblog_id=10 AND author_id={embed:author_id} ORDER BY entry_date DESC LIMIT 5"}
- ...news headlines...
- {/exp:query}
Voila! Embedded content. Granted, the SQL part is daunting if you're new to the Tao of the database, but with a little studying, anyone can cook up a userful query.






Comments
August 20, 2008
10:11 am
August 21, 2008
11:07 am
August 21, 2008
3:20 pm
August 21, 2008
4:31 pm
August 21, 2008
4:58 pm
August 21, 2008
5:01 pm
Whaddya think?