1

Sorry if this question is starting off too abstract. I'm porting a calendar plugin from Textpattern and WordPress into Craft. Basic workflow for the output has been (in twig terms):

{% set start = "now"|date("Y-m-d") %}
{{ craft.Calendar37.calendar_full(start)|raw }}

and the plugin php does all the work of querying the db and producing the calendar.

What I'm up against is getting the db query right. It seems pretty abstract to get all the fields related to an entry--in particular working out the query to get the category from a category field in the entry. Probably I can work that out, but here's my question:

Would it be possible or better to do more of the work with Twig, and less in php? I.e. once the php db query knows the entries and i could put that into (I suppose) a model, then can Twig figure out the rest using (I haven't tried this, just working it out in my mind):

{% set start = "now"|date("Y-m-d") %}
{% craft.Calendar37.initialize(start) %}  {# just does the DB query #}

<table>
  ...
  <td>
    {% set event_instances = craft.Calendar37.getInstances(date) %}
    {% for instance in event_instances %}
       {% set entry = craft.entries.id(instance.entryId) %}
       <li title='{{ entry.tip }}'>
          <a href='{{ siteUrl }}{{ entry.category }}'>
             {{ instance.time }} - {{ craft.entries.id(instance.entryId).title }}
          </a>
       </li>
    {% endfor %}
  <td>

I wonder about efficiency. One big, but precise query in php is probably better than going back from twig for every event on each day of the month as shown in the second part here. This is the query so far; it doesn't find the category, or slug, or several other things yet:

$events = $query
    ->select('*') //c37.event_id, dateYmd, timestr, alt_text, css_class, entries.id as event_id, title as event_title, slug')
    ->from('calendar37 c37')
    ->join('entries entries', 'c37.event_id = entries.id')
    ->join('content content', 'entries.id = content.elementId')
    ->join('elements_i18n elements_i18n', 'entries.id = elements_i18n.elementId')
    ->where("c37.dateYmd >= $fromDateYmd")
    ->andWhere("c37.dateYmd <= $toDateYmd") 
    //->andWhere('STATUS = published, pending, etc')
    ->order('dateYmd ASC, timestr ASC')
    ->queryAll();

Thanks for any advice!

1

I wonder about efficiency.

You're right to worry. Doing this from PHP (even if you break it up into multiple SQL queries to get the data you need) is going to be much faster and less resource intensive than doing it from Twig.

One thing I'd look at though, instead of querying data directly against the database, is to see if you can get what you need going through Craft's services in craft/app/services/.

That way you don't have to worry about your plugin breaking in the event that the underlying database structure changes during an update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.