PHP SQL with NoSQL

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsPHP SQL with NoSQL
m (Added syntax highlighting)
Line 1: Line 1:
<pre>
+
<source lang=php>
 
// script db._table.contact.get.post_process
 
// script db._table.contact.get.post_process
  
Line 10: Line 10:
 
if (isset($content['resource']) && !empty($content['resource'])) {
 
if (isset($content['resource']) && !empty($content['resource'])) {
  
        foreach($content['resource'] as $k => $record){
+
    foreach($content['resource'] as $k => $record){
                // filter by email
+
        // filter by email
                $params = [
+
        $params = [
                      'filter' => 'email=' . $record['email']
+
              'filter' => 'email=' . $record['email']
                ];
+
        ];
  
                // get matching record from MongoDB service
+
        // get matching record from MongoDB service
                $result = $get('mongodb/_table/contact', $params);
+
        $result = $get('mongodb/_table/contact', $params);
  
                // from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be                 
+
        // from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be                 
                $record['from_mongo_twitter'] = $result['content']['resource'][0]['twitter'];
+
        $record['from_mongo_twitter'] = $result['content']['resource'][0]['twitter'];
                $event['response']['content']['resource'][$k] = $record;
+
        $event['response']['content']['resource'][$k] = $record;
  
                // set this flag if you change the response content
+
        // set this flag if you change the response content
// you can also set $event['response']['status_code'] and $event['response']['content_type']
+
        // you can also set $event['response']['status_code'] and $event['response']['content_type']
                $event['response']['content_changed'] = true;
+
        $event['response']['content_changed'] = true;
        }
+
    }
 
}
 
}
</pre>
+
 
 +
</source>

Revision as of 23:23, 5 May 2016

// script db._table.contact.get.post_process
 
// for each record in the MySQL db service, query the MongoDB service to get the Twitter handle for that contact
 
$content = $event['response']['content'];
$api = $platform['api'];
$get = $api-&gt;get;
 
if (isset($content['resource']) &amp;&amp; !empty($content['resource'])) {
 
    foreach($content['resource'] as $k =&gt; $record){
        // filter by email
        $params = [
               'filter' =&gt; 'email=' . $record['email']
        ];
 
        // get matching record from MongoDB service
        $result = $get('mongodb/_table/contact', $params);
 
        // from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be                
        $record['from_mongo_twitter'] = $result['content']['resource'][0]['twitter'];
        $event['response']['content']['resource'][$k] = $record;
 
        // set this flag if you change the response content
        // you can also set $event['response']['status_code'] and $event['response']['content_type']
        $event['response']['content_changed'] = true;
    }
}