Answers.Body: <p>That solution makes sense! I have used a similar strategy. You should absolutely cache around the template hook, or cache the database calls in the plugin itself. </p>
  
  <p>Take a look at the <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder" rel="nofollow">Yii Query Builder</a> to see how Yii/Craft handles MySQL queries. You can also look at examples <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/ElementsService.php#L142" rel="nofollow">Craft source code</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T14:33:52.313Z
Answers.LastActivityDate: 2015-06-23T14:33:52.313Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <p>I'm looking at how best to incorporate various sets of data from another MySQL database into a Craft site. Using <a href="https://github.com/engram-design/FeedMe" rel="nofollow noreferrer">the Feed Me plugin</a> would be one way, if the data source had feeds, but I'm thinking about alternatives. Does this make sense:</p>
  
  <ol>
  <li>There is an Entry whose page should display data from the external DB.</li>
  <li>The admin user can, using standard Fields, select the criteria for which data it is (eg, category, number of items, timespan, etc).</li>
  <li>In the Entry's template, a <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow noreferrer">template hook</a> calls a Craft plugin.</li>
  <li>The plugin makes a request to the external DB, tidies the returned data, and renders it into the page with a twig template.</li>
  </ol>
  
  <p>Does that make sense? Anything that sounds impossible or stupid? Can you think of a better solution?</p>
  
  <p>It <a href="https://craftcms.stackexchange.com/questions/10287/template-hook-caching">looks like</a> I could cache that part of the template so that it didn't make requests to the external DB with every page request. Is that right?</p>
  
  <p>(For context: I haven't used hooks yet, and have only written the most basic of Craft plugins. I have years of general PHP experience, but little with Craft or Yii.)</p>
  
Tags: <plugins><database><hook>
Title: Incorporating data from an external database into Craft pages
ThreadText: # Incorporating data from an external database into Craft pages
  
  (Asked by: username_0 on 2015-06-23)
  
  I'm looking at how best to incorporate various sets of data from another MySQL database into a Craft site. Using [the Feed Me plugin](https://github.com/engram-design/FeedMe) would be one way, if the data source had feeds, but I'm thinking about alternatives. Does this make sense:
  
  1. There is an Entry whose page should display data from the external DB.
  2. The admin user can, using standard Fields, select the criteria for which data it is (eg, category, number of items, timespan, etc).
  3. In the Entry's template, a [template hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail) calls a Craft plugin.
  4. The plugin makes a request to the external DB, tidies the returned data, and renders it into the page with a twig template.
  
  Does that make sense? Anything that sounds impossible or stupid? Can you think of a better solution?
  
  It [looks like](https://craftcms.stackexchange.com/questions/10287/template-hook-caching) I could cache that part of the template so that it didn't make requests to the external DB with every page request. Is that right?
  
  (For context: I haven't used hooks yet, and have only written the most basic of Craft plugins. I have years of general PHP experience, but little with Craft or Yii.)
  
  **Tags:** plugins, database, hook
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-06-23. Score: 4 | Accepted answer
  
  That solution makes sense! I have used a similar strategy. You should absolutely cache around the template hook, or cache the database calls in the plugin itself.
  
  Take a look at the [Yii Query Builder](http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder) to see how Yii/Craft handles MySQL queries. You can also look at examples [Craft source code](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/ElementsService.php#L142).
  
  
  ---
  
  