<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>labs.boulevart</title>
	<atom:link href="http://labs.boulevart.be/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://labs.boulevart.be</link>
	<description>your multi media production partner</description>
	<pubDate>Tue, 02 Nov 2010 17:09:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>Quick guide to the new Drupal 7 database layer</title>
		<link>http://labs.boulevart.be/index.php/2010/11/02/quick-guide-to-the-new-drupal-7-database-layer/</link>
		<comments>http://labs.boulevart.be/index.php/2010/11/02/quick-guide-to-the-new-drupal-7-database-layer/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 17:09:08 +0000</pubDate>
		<dc:creator>Jeroen</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[api]]></category>

		<category><![CDATA[Database]]></category>

		<category><![CDATA[Drupal]]></category>

		<category><![CDATA[Drupal 7]]></category>

		<category><![CDATA[Drupal 7 database API]]></category>

		<guid isPermaLink="false">http://labs.boulevart.be/?p=704</guid>
		<description><![CDATA[
This post is my effort to guide you through the whole new abstraction layer for accessing the database server, which comes with the Drupal 7 core distribution. In this blog-post I’ll try to explain to you guys (girls) how this new layer works, without diving into all the details, in order to give you a [...]]]></description>
			<content:encoded><![CDATA[<p><!-- p.p1 {margin: 0.0px 0.0px 17.0px 0.0px; line-height: 19.0px; font: 16.0px 'Lucida Grande'} p.p2 {margin: 0.0px 0.0px 13.0px 0.0px; line-height: 19.0px; font: 13.0px 'Lucida Grande'} p.p3 {margin: 0.0px 0.0px 13.0px 0.0px; line-height: 19.0px; font: 13.0px 'Lucida Grande'; min-height: 16.0px} --></p>
<p class="p1">This post is my effort to guide you through the whole new abstraction layer for accessing the database server, which comes with the Drupal 7 core distribution. In this blog-post I’ll try to explain to you guys (girls) how this new layer works, without diving into all the details, in order to give you a clear view on this new system.</p>
<p class="p2">First of all I’ll explain the benefits of this new system to you. Drupal needed a database system that could easily support multiple database servers in a unified way which preserves the syntax power of SQL. The system was also build to enforce the security checks. There are 6 different types of Query’s which can be called in this database system: Insert, Update, Delete, Merge, Static and Dynamic.</p>
<p class="p2">Now lets get it started!</p>
<h3>Settings.php</h3>
<p class="p2">We should start by taking a look at the renewed database definition in the settings.php. In most cases you will be using a drupal site with only one database. For this you should use the following structure.</p>
<p class="p2" style="text-align: justify; "><span style="color: #3366ff;">&lt;?php<br />
$databases</span><span style="color: #339966;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">][</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">] = array(</span><br />
<span style="color: #ff0000;"> &#8216;driver&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;mysql&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;database&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;drupaldb&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;username&#8217;</span><span style="color: #339966;"> =&gt;</span><span style="color: #ff0000;"> &#8216;username&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;password&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8217;secret&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;host&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;localhost&#8217;</span><span style="color: #339966;">,<br />
);</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">The first thing you should notice are the values “default” in the $database array. The first one is the CONNECTION KEY, the second one is the TARGET.</p>
<p class="p2">The <em>connection key</em> is a unique identifier for a database connection, there must ALWAYS be a ‘default’ connection key available.</p>
<p class="p2">The <em>target</em> is used to define master/slave database structures. If the master (default) isn’t available, the system will search for the slave database. It is also possible to flag a query to run on the slave database. To make this a bit clearer I’ll give you a more complicated database structure.</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #339966;"> $databases[</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">][</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">] = array(</span><br />
<span style="color: #ff0000;"> &#8216;driver&#8217;</span><span style="color: #339966;"> =&gt; &#8216;</span><span style="color: #ff0000;">mysql&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;database&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;drupaldb1&#8242;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;username&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;username&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;password&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8217;secret&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;host&#8217;</span><span style="color: #339966;"> =&gt; &#8216;</span><span style="color: #ff0000;">dbserver1&#8242;</span><span style="color: #339966;">,<br />
);<br />
$databases[</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">][</span><span style="color: #ff0000;">'slave'</span><span style="color: #339966;">][] = array(</span><br />
<span style="color: #ff0000;"> &#8216;driver&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;mysql&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;database&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;drupaldb2&#8242;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;username&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;username&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;password&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8217;secret&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;host&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;dbserver2</span><span style="color: #339966;">&#8216;,<br />
);</span><br />
<span style="color: #3366ff;">?&gt;</span></p>
<p class="p2">In this case, the first database is the default database, the second is the slave databases.</p>
<p class="p2">It is also possible to define separate database structures.</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #339966;">$databases[</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">][</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">] = array(</span><br />
<span style="color: #ff0000;">&#8216;driver&#8217;</span><span style="color: #339966;"> =&gt; &#8216;</span><span style="color: #ff0000;">mysql&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;database&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;drupaldb1&#8242;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;username&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;username&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;password&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8217;secret&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;host&#8217;</span><span style="color: #339966;"> =&gt; &#8216;</span><span style="color: #ff0000;">dbserver1&#8242;</span><span style="color: #339966;">,<br />
);<br />
$databases[</span><span style="color: #ff0000;">'extra'</span><span style="color: #339966;">][</span><span style="color: #ff0000;">'default'</span><span style="color: #339966;">][] = array(</span><br />
<span style="color: #ff0000;">&#8216;driver&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;mysql&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;database&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;drupaldb2&#8242;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;username&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;username&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;password&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8217;secret&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;">&#8216;host&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;dbserver2</span><span style="color: #339966;">&#8216;,<br />
);</span><br />
<span style="color: #3366ff;">?&gt;</span></p>
<p><span style="font-family: 'Lucida Grande'; font-weight: normal; line-height: normal; font-size: 13px;">Note that no matter how many connections are defined in the settings file, These connections will not be used by Drupal until they are actually opened.</span></p>
<p class="p2">
<h2>Select Query</h2>
<p class="p2">So far for the database connections, now lets take a look at the actual use of query’s in this new database layer. For the regular select query’s not much changes. Here is an example of a select query with a short explanation.</p>
<p class="p2"><span style="color: #3366ff;">$result</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">db_query</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8220;SELECT nid, title FROM {node} WHERE type = :type&#8221;</span><span style="color: #339966;">, </span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;:type&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;page&#8217;</span><span style="color: #339966;">,<br />
));</span></p>
<p class="p2">The db_query function uses three arguments, first one is the query string, the second one are the values used to fill up the placeholders. The third one will be explained at the next example.</p>
<p class="p2">Note that the placeholder (:type) doesn’t use quotes. Another thing you should take in to account is to put your database names between {}. This is needed for the database system to attache a prefix string if this is defined in your settings.</p>
<p class="p2">Now for the third argument of the db_query we will take a look at the following code:</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> $result</span><span style="color: #339966;"> =</span><span style="color: #3366ff;"> db_query</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8220;SELECT nid, title FROM {node}&#8221;</span><span style="color: #339966;">, </span><span style="color: #3366ff;">array</span><span style="color: #339966;">(), </span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;target&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8217;slave&#8217;</span><span style="color: #339966;">,<br />
));</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">The third argument is an array of configuration directives to detect the way the query should run. In this case the query runs on the slave database. I won’t go into the details here, because (as i said before) in this blog post I will only pick up the basics of the new database system.</p>
<p class="p2">The following is just a handy guide about the way you can use the database query’s results. (Not 100% relevant to this post, but this might come in handy for some of you. The others should just scroll through the code, as if it doesn’t exist).</p>
<p class="p2"><span style="color: #3366ff;">$result</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">db_query</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8220;SELECT nid, title FROM {node}&#8221;</span><span style="color: #339966;">);</span><br />
<span style="color: #3366ff;">foreach</span><span style="color: #339966;"> (</span><span style="color: #3366ff;">$result</span><span style="color: #339966;"> as </span><span style="color: #3366ff;">$record</span><span style="color: #339966;">) {</span><br />
<span style="color: #ff9900;"> // Do something with each $record<br />
</span><span style="color: #339966;"> }</span></p>
<p class="p2"><span style="color: #3366ff;">$record</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">$result</span><span style="color: #339966;">-&gt;</span><span style="color: #3366ff;">fetch</span><span style="color: #339966;">(); </span><span style="color: #ff9900;">// Use the default fetch mode.</span></p>
<p class="p2"><span style="color: #3366ff;">$record</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">$result</span><span style="color: #339966;">-&gt;</span><span style="color: #3366ff;">fetchObject</span><span style="color: #339966;">(); </span><span style="color: #ff9900;">// Fetch as a stdClass object</span></p>
<p class="p2"><span style="color: #3366ff;">$record</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">$result</span><span style="color: #339966;">-&gt;</span><span style="color: #3366ff;">fetchField</span><span style="color: #339966;">(</span><span style="color: #3366ff;">$column_index</span><span style="color: #339966;">); </span><span style="color: #ff9900;">// Fetch only one field.</span></p>
<p class="p2"><span style="color: #3366ff;">$number_of_rows</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">$result</span><span style="color: #339966;">-&gt;</span><span style="color: #3366ff;">rowCount</span><span style="color: #339966;">(); </span><span style="color: #ff9900;">//Count the results.</span></p>
<p class="p3">
<h2>Insert Query</h2>
<p class="p2">Now we’ve arrived to he fun part. The INSERT, DELETE and UPDATE query’s require that you use the query builder object in order to behave consistently across all different databases. This is where the new object-oriented query API comes in.</p>
<p class="p2">The compact INSERT form is the following:</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> $nid</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">db_insert</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;node&#8217;</span><span style="color: #339966;">)</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;title&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;Example&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;uid&#8217;</span><span style="color: #339966;"> =&gt; 1,</span><br />
<span style="color: #ff0000;"> &#8216;created&#8217; </span><span style="color: #339966;">=&gt; </span><span style="color: #3366ff;">REQUEST_TIME</span><span style="color: #339966;">,</span><br />
<span style="color: #339966;"> ))</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">This will result in the following query:</p>
<blockquote>
<p class="p2"><span style="color: #808080;">INSERT INTO {node} (title, uid, created) VALUES (&#8217;Example&#8217;, 1, 1221717405);</span></p>
</blockquote>
<p class="p2">Note: If you don’t call the execute() method, the query will not run!</p>
<p class="p2">The insert query object can also be used with multiple values. To insert multiple rows you shouldn’t only use fields() but also values(). In this case fields() only defines the fields, but doesn’t put any content into the selected fields. The values() may be called multiple times in order to add more than one line to your database.</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> $query</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">db_insert</span><span style="color: #339966;">(&#8217;node&#8217;)</span></p>
<p class="p2"><span style="color: #339966;">-&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;title&#8217;</span><span style="color: #339966;">, </span><span style="color: #ff0000;">&#8216;uid&#8217;</span><span style="color: #339966;">, </span><span style="color: #ff0000;">&#8216;created&#8217;</span><span style="color: #339966;">))<br />
-&gt;</span><span style="color: #3366ff;">values</span><span style="color: #339966;">(array(</span><br />
<span style="color: #3366ff;"> array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;title&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;Example&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;uid&#8217;</span><span style="color: #339966;"> =&gt; 1,</span><br />
<span style="color: #ff0000;"> &#8216;created&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">REQUEST_TIME</span><span style="color: #339966;">,</span><br />
<span style="color: #339966;"> ),</span><br />
<span style="color: #3366ff;"> array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;title&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;Example 2&#8242;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;uid&#8217;</span><span style="color: #339966;"> =&gt; 1,</span><br />
<span style="color: #ff0000;"> &#8216;created&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">REQUEST_TIME</span><span style="color: #339966;">,<br />
)))<br />
-&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">Using a ‘foreach’ the code will look like this:</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> $values</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #3366ff;"> array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;title&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;Example&#8217;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;uid&#8217;</span><span style="color: #339966;"> =&gt; 1,</span><br />
<span style="color: #ff0000;"> &#8216;created&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">REQUEST_TIME</span><span style="color: #339966;">,</span><br />
<span style="color: #339966;"> ),</span><br />
<span style="color: #3366ff;"> array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;title&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #ff0000;">&#8216;Example 2&#8242;</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;uid&#8217;</span><span style="color: #339966;"> =&gt; 1,</span><br />
<span style="color: #ff0000;"> &#8216;created&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">REQUEST_TIME</span><span style="color: #339966;">,</span><br />
<span style="color: #339966;"> ),</span><br />
<span style="color: #339966;"> );</span><br />
<span style="color: #3366ff;"> $query</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">db_insert</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;node&#8217;</span><span style="color: #339966;">)-&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;title&#8217;</span><span style="color: #339966;">, </span><span style="color: #ff0000;">&#8216;uid&#8217;</span><span style="color: #339966;">, </span><span style="color: #ff0000;">&#8216;created&#8217;</span><span style="color: #339966;">));</span><br />
<span style="color: #3366ff;">foreach</span><span style="color: #339966;"> (</span><span style="color: #3366ff;">$values</span><span style="color: #339966;"> as </span><span style="color: #3366ff;">$record</span><span style="color: #339966;">) {</span><br />
<span style="color: #3366ff;"> $query</span><span style="color: #339966;">-&gt;</span><span style="color: #3366ff;">values</span><span style="color: #339966;">(</span><span style="color: #3366ff;">$record</span><span style="color: #339966;">);</span><br />
<span style="color: #339966;"> }</span><br />
<span style="color: #3366ff;"> $query</span><span style="color: #339966;">-&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p3">
<h2>Update Query</h2>
<p class="p2">Next stop is the UPDATE query. The update query is pretty straight forward, if you understand how the insert query’s work, it shouldn’t be a problem to understand the update query. Here it goes:</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> $num_updated</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">db_update</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;node&#8217;</span><span style="color: #339966;">)</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;uid&#8217;</span><span style="color: #339966;"> =&gt; 5,</span><br />
<span style="color: #ff0000;"> &#8217;status&#8217;</span><span style="color: #339966;"> =&gt; 1,</span><br />
<span style="color: #339966;"> ))</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">condition</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;created&#8217;</span><span style="color: #339966;">, </span><span style="color: #3366ff;">REQUEST_TIME</span><span style="color: #339966;"> - 3600, &#8216;&gt;=&#8217;)</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">This will result in the following query:</p>
<blockquote>
<p class="p2"><span style="color: #808080;">UPDATE {node} SET uid=5, status=1 WHERE created &gt;= 1221717405;</span></p>
</blockquote>
<p class="p2">Not much to explain here, so let’s go to the DELETE query’s.</p>
<p class="p2">
<h2>Update Query</h2>
<p class="p2">Again the same story here. The DELETE query is probabily the easiest form of the query object:</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php<br />
$num_deleted</span><span style="color: #339966;"> = </span><span style="color: #3366ff;">db_delete</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;node&#8217;</span><span style="color: #339966;">)</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">condition</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;nid&#8217;</span><span style="color: #339966;">, 5)</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">This will result in the following query:</p>
<blockquote>
<p class="p2"><span style="color: #808080;">DELETE FROM {node} WHERE nid=5;</span></p>
</blockquote>
<p class="p2">
<h2>Update Query</h2>
<p class="p2">Finally we’ve got to the last one. The MERGE query. This one is a bit more complicated. If you would strip this one down to it’s original form, you will finde that a merge query is actually just the combination of an insert and an update query. In php it would be something like this:</p>
<blockquote>
<p class="p2"><span style="color: #808080;">&lt;?php<br />
if (db_query(&#8221;SELECT COUNT(*) FROM {example} WHERE id=:id&#8221;, array(&#8217;:id&#8217; =&gt; $id)-&gt;fetchField()) {<br />
// Run an update using WHERE id = $id<br />
}<br />
else {<br />
// Run an insert, inserting $id for id<br />
}<br />
?&gt;</span></p></blockquote>
<p class="p2">In the new database API structure the merge query’s are build up like this:</p>
<p class="p2"><span style="color: #0000ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> db_merge</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;example&#8217;</span><span style="color: #339966;">)<br />
-&gt;</span><span style="color: #3366ff;">key</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;name&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$name</span><span style="color: #339966;">))<br />
-&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;field1&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value1</span><span style="color: #339966;">,<br />
&#8216;</span><span style="color: #ff0000;">field2&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value2</span><span style="color: #339966;">,<br />
))<br />
-&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #0000ff;"> ?&gt;</span></p>
<p class="p2">Here the “example” table is used. The specified key field ‘name’ has the value of $name. Now two things could happen.</p>
<p class="p2">First option: If the $name value exists in the database, then fields “field1” and “field2” will get an update with the correspondingvalues.</p>
<p class="p2">Second option: If the $name value doesn’t exist in the database, a new row will be created in which “name” gets the value $name, “field1” gets the value $field1 and “field2” gets the value $field2.</p>
<p class="p2">In some cases the values you want to set will have to be different, according to the fact that the key field does or doesn’t already exist. This can be handled in two ways.</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> db_merge</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;example&#8217;</span><span style="color: #339966;">)<br />
-&gt;</span><span style="color: #3366ff;">key</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;name&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$name</span><span style="color: #339966;">))<br />
-&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;field1&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value1</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;field2&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value2</span><span style="color: #339966;">,<br />
))<br />
-&gt;</span><span style="color: #3366ff;">update</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;field1&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$alternate1</span><span style="color: #339966;">,<br />
))<br />
-&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">In this case, if the “name” already exists the value of “field1” will be $alternate1, and the value of “field2” will be $value2. If the “name” doesn’t allready exist, $value1 and $value2 will be used.</p>
<p class="p2">It is also possible to use expressions. I’ll give you an example in which, if the ‘name’ already exists, the “value1” field will become the current value +1:</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php<br />
db_merge</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;example&#8217;</span><span style="color: #339966;">)</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">key</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;name&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$name</span><span style="color: #339966;">))</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(</span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><br />
<span style="color: #ff0000;"> &#8216;field1&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value1</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;field2&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value2</span><span style="color: #339966;">,</span><br />
<span style="color: #339966;"> ))</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">expression</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;field1&#8242;</span><span style="color: #339966;">, </span><span style="color: #ff0000;">&#8216;field1 + :inc&#8217;</span><span style="color: #339966;">, </span><span style="color: #3366ff;">array</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;:inc&#8217;</span><span style="color: #339966;"> =&gt; 1))</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">Note that expression() can be used multiple times, 1 time for each field.</p>
<p class="p2">Field updates can also be limited, if the row already exists. In this case, if the “name” already exists, only “field2” will be updated, and “field1” will be ignored:</p>
<p class="p2"><span style="color: #3366ff;">&lt;?php</span><br />
<span style="color: #3366ff;"> db_merge</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;example&#8217;</span><span style="color: #339966;">)<br />
-&gt;</span><span style="color: #3366ff;">key</span><span style="color: #339966;">(array(</span><span style="color: #ff0000;">&#8216;name&#8217;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$name</span><span style="color: #339966;">))<br />
-&gt;</span><span style="color: #3366ff;">fields</span><span style="color: #339966;">(array(</span><br />
<span style="color: #ff0000;"> &#8216;field1&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value1</span><span style="color: #339966;">,</span><br />
<span style="color: #ff0000;"> &#8216;field2&#8242;</span><span style="color: #339966;"> =&gt; </span><span style="color: #3366ff;">$value2</span><span style="color: #339966;">,</span><br />
<span style="color: #339966;"> ))</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">updateExcept</span><span style="color: #339966;">(</span><span style="color: #ff0000;">&#8216;field1&#8242;</span><span style="color: #339966;">)</span><br />
<span style="color: #339966;"> -&gt;</span><span style="color: #3366ff;">execute</span><span style="color: #339966;">();</span><br />
<span style="color: #3366ff;"> ?&gt;</span></p>
<p class="p2">This is the end of my quick guide into the new Drupal 7 database layer. Hope you&#8217;ve enjoyed it. For more detailed information about this system, I would like to refere to the official drupal database API: <a href="http://drupal.org/developing/api/database">http://drupal.org/developing/api/database</a></p>
<p><!--EndFragment--></p>
]]></content:encoded>
			<wfw:commentRss>http://labs.boulevart.be/index.php/2010/11/02/quick-guide-to-the-new-drupal-7-database-layer/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Action View Helper</title>
		<link>http://labs.boulevart.be/index.php/2010/10/12/the-action-view-helper/</link>
		<comments>http://labs.boulevart.be/index.php/2010/10/12/the-action-view-helper/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 14:43:33 +0000</pubDate>
		<dc:creator>Yves</dc:creator>
		
		<category><![CDATA[Zend]]></category>

		<category><![CDATA[action helper]]></category>

		<category><![CDATA[action view helper]]></category>

		<category><![CDATA[layout]]></category>

		<category><![CDATA[view helper]]></category>

		<guid isPermaLink="false">http://labs.boulevart.be/?p=701</guid>
		<description><![CDATA[Situation:
I use Zend_Layout to create a global view template for my website and the view (.phtml) that corresponds to a controller/action will be loaded into the $this-&#62;layout-&#62;content variable automatically.
All good but what to do with a form in your global template?
Building the form (Zend_Form) into your view isn’t really a clean option and what about [...]]]></description>
			<content:encoded><![CDATA[<h2>Situation:</h2>
<p>I use Zend_Layout to create a global view template for my website and the view (.phtml) that corresponds to a controller/action will be loaded into the $this-&gt;layout-&gt;content variable automatically.</p>
<p>All good but what to do with a form in your global template?</p>
<p>Building the form (Zend_Form) into your view isn’t really a clean option and what about validation  of the form? There is no controller/action from where the form was build and so there is no controller/action where to post the form to.</p>
<h2>Solution:</h2>
<p>I read through the <strong>Zend</strong> manual and I discovered there was a view helper named ‘Action’.</p>
<p>What does it do?</p>
<p>It enables you to call a controller/action from within your view script. It will process the controller/action code, render the view and return it into your view script.</p>
<h2>Example:</h2>
<p>IndexController.php</p>
<p>class IndexController extends Zend_Controller_Action {</p>
<p>public function indexAction(){</p>
<p>}</p>
<p>public function formAction(){</p>
<p>//build a form</p>
<p>$form = new Zend_Form();</p>
<p>//create a text element</p>
<p>$elementName = new Zend_Form_Element_Text(’name’);</p>
<p>$element-&gt;setRequired(true);    //a validation saying our field has to be filled out</p>
<p>//create a submit button</p>
<p>$elementSubmit = new Zend_Form_Element_Submit(’submit’);</p>
<p>$elementSubmit-&gt;setLabel(’Send’);</p>
<p>//add elements to the form</p>
<p>$form-&gt;addElement($elementName);</p>
<p>$form-&gt;addElement($elementSubmit);</p>
<p>//if we click the ‘Send’ button</p>
<p>if($this-&gt;getRequest()-&gt;isPost()){</p>
<p>if($form-&gt;isValid()){</p>
<p>//form is valid</p>
<p>}</p>
<p>}</p>
<p>//add the form to a view variable</p>
<p>$this-&gt;view-&gt;form = $form;</p>
<p>}</p>
<p>}</p>
<p>index/form.phtml</p>
<p>&lt;?php echo this-&gt;form; ?&gt;</p>
<p>layout.phtml</p>
<p>&lt;?php echo$this-&gt;doctype(Zend_View_Helper_Doctype::XHTML1_STRICT) . “\n”; ?&gt;</p>
<p>&lt;html&gt;</p>
<p>&lt;head&gt;</p>
<p>&lt;/head&gt;</p>
<p>&lt;body&gt;</p>
<p>&lt;div id=”container”&gt;</p>
<p>&lt;?phpecho$this-&gt;layout()-&gt;content.”\n”;?&gt;</p>
<p>&lt;/div&gt;</p>
<p>&lt;div&gt;</p>
<p><strong>&lt;?php echo $this-&gt;action(’form’, ‘index’); ?&gt;</strong></p>
<p>&lt;/div&gt;</p>
<p>&lt;/body&gt;</p>
<p>&lt;/html&gt;</p>
<p>parameter list of the view action</p>
<p>action($action, $controller, $module = null, array $params = array())</p>
<h2>Result:</h2>
<p>The form action of the index controller is called from the layout.phtml.</p>
<p>The business logic is executed, the view (index/form.phtml) is rendered and is returned to our layout.phtml.</p>
<p>When we press the ‘Send’ button in our form. The form will be submitted to index/form and the validation will happen.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.boulevart.be/index.php/2010/10/12/the-action-view-helper/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP and UTF-8: The way to success</title>
		<link>http://labs.boulevart.be/index.php/2010/10/11/php-and-utf-8-the-way-to-success/</link>
		<comments>http://labs.boulevart.be/index.php/2010/10/11/php-and-utf-8-the-way-to-success/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 18:11:40 +0000</pubDate>
		<dc:creator>Yves</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[amfphp]]></category>

		<category><![CDATA[doctrine]]></category>

		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://labs.boulevart.be/?p=699</guid>
		<description><![CDATA[1) Making sure the HTML file is saved as UTF-8
2) Changing the charset of the HTML file in the &#60;head&#62; section
3) Sent a header via PHP
4) Using the mb_string module for apache
5) The Database
Extra
1) Communication with Doctrine
2) Communication with AMFPHP
1) Making sure the HTML file is saved as UTF-8
Displaying UTF-8 characters in the browser.
First we [...]]]></description>
			<content:encoded><![CDATA[<p>1) Making sure the HTML file is saved as UTF-8</p>
<p>2) Changing the charset of the HTML file in the &lt;head&gt; section</p>
<p>3) Sent a header via PHP</p>
<p>4) Using the mb_string module for apache</p>
<p>5) The Database</p>
<p>Extra</p>
<p>1) Communication with Doctrine</p>
<p>2) Communication with AMFPHP</p>
<h2><strong>1) Making sure the HTML file is saved as UTF-8</strong></h2>
<p>Displaying UTF-8 characters in the browser.</p>
<p>First we make sure that when we save an html file that contains UTF-8 characters, it really is saved as UTF-8 text. I use Eclipse as IDE and you will find the setting at the following location:</p>
<ul>
<li>Windows: Window &gt; Preferences… &gt; General &gt; Workspace : Text file encoding</li>
<li>Mac: Eclipse &gt; Preferences… &gt; General &gt; Workspace : Text file encoding</li>
</ul>
<p>Check the ‘Other’ radio button and choose ‘UTF-8′ from the dropdown list. Press the ‘OK’ button</p>
<h2><strong>2) Changing the charset of the HTML file in the &lt;head&gt; section</strong></h2>
<p><em><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;</p>
<p>&lt;html&gt;</p>
<p>&lt;head&gt;</p>
<p>&lt;META http-equiv="Content-Type" content="text/html; <strong>charset=utf-8</strong>” /&gt;</p>
<p>&lt;/head&gt;</p>
<p>&lt;body&gt;</code></em></p>
<p>When we make an HTML file and use UTF-8 characters, it will display the characters correctly.</p>
<p><strong>Using a PHP server to generate a page</strong></p>
<p>When we use a PHP server to process the php code and receive HTML there is another catch we have to keep in mind. The server could have a server encoding setting which could screw up the UTF-8 characters. We solve this by sending a header in the php script.</p>
<h2><strong>3) Sent a header via PHP</strong></h2>
<p>At the top of our page we put the following PHP code:</p>
<p><em><code>header('Content-type: text/html; charset=utf-8');</code></em></p>
<p>The server which processes the page now also sends the UTF-8 characters as UTF-8 to the client.</p>
<p><strong>Manipulating the UTF-8 data in PHP</strong></p>
<p>Everybody uses the strlen() function to get the length of a string. Now there is a problem when using the strlen() function on an UTF-8 string. The strlen() function reads the number of bytes instead of the number of characters. So when we execute the command : <strong>echo strlen(’être’);</strong></p>
<p>It will return 5 and not 4 as we expected. This is because the character <strong>ê </strong>is a character of 2 bytes.</p>
<p>Obviously this is not the behavior we want.</p>
<h2><strong>4) Using the mb_string module for apache</strong></h2>
<p>The solution to this problem is installing a module <strong>‘mb_string’ </strong>which has to be loaded into apache.</p>
<p>It is a module with a set of standard functions that can operate on multi byte strings (such as UTF-8)</p>
<p>More info can be found at <a href="http://be2.php.net/mb_string">http://be2.php.net/mb_string</a></p>
<p>So instead of using strlen() we use mb_strlen().</p>
<p>But also here there is a catch. The module ‘mb_string’ has an internal encoding setting.</p>
<p>We have to set the internal encoding to UTF-8.</p>
<p>There are 2 ways for doing this</p>
<p>1) add the string ‘UTF-8′ as second parameter to the function</p>
<p><em><code>echo mb_strlen('être',<strong>‘UTF-8′</strong>);</code></em></p>
<p>2) or changing the ini setting via the commmand</p>
<p><em><code>ini_set('mbstring.internal_encoding','UTF-8');</code></em></p>
<h2><strong>5) The database</p>
<p></strong></h2>
<p>The final step in our <strong>way</strong> to <strong>success</strong> is setting up the database correctly.</p>
<p>You have to make sure that your table + the fields have a collation that supports UTF-8 such as <strong>utf8_general_ci</strong></p>
<p>This should do the trick to display and manipulate UTF-8 based characters in PHP.</p>
<p><strong>EXTRA (Thanks to Filip Heymans)</p>
<p></strong></p>
<p>1) Communication with Doctrine</p>
<p>2) Communication with AMFPHP</p>
<h2><strong>1) Communication with Doctrine</strong></h2>
<p>Doctrine is a persistence framework for PHP. Instead of using SQL to create CRUD operations, you will create objects and perform actions on those objects. More info at <a href="http://www.doctrine-project.org" target="_blank">http://www.doctrine-project.org</a>.</p>
<p>It is simular to Hibernate for Java.</p>
<p>When considering using doctrine, you must also set a property to the UTF-8 encoding or the insert and update statements will not produce the expected result.</p>
<p>Doctrine uses a connection object. You have to set the charset on that connection object via the setCharset method.<em><code><span class="default"></p>
<p>$conn</span><span class="default"> </span><span class="keyword">=</span><span class="default"> </span><span class="default">Doctrine_Manager</span><span class="keyword">::</span><span class="default">connection</span><span class="keyword">(</span><span class="string">‘mysql://username:password@localhost/test’</span><span class="keyword">)</span></p>
<p><strong>$conn-&gt;setCharset(’<strong>UTF8</strong>′);</strong></code></em></p>
<h2><strong><strong>2) Communication with Amfphp</strong></strong></h2>
<p><a href="http://www.amfphp.org/">AMFPHP</a> is a free open-source <a href="http://www.php.net/">PHP</a> implementation of the <a href="http://en.wikipedia.org/wiki/Action_Message_Format">Action Message <span class="GramE">Format(</span>AMF)</a>. AMF allows for binary serialization of <a href="http://en.wikipedia.org/wiki/Actionscript">Action Script</a> (AS2, AS3) native types and objects to be sent to server side services.</p>
<p>In the gateway.php file in the amphp folder. You have to find the rule</p>
<p><em><code></p>
<p>$gateway-&gt;setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");</code></em></p>
<p>and change it to</p>
<p><em><code>$gateway-&gt;setCharsetHandler("none", "UTF-8", "UTF-8");</code></em></p>
<p>This makes sure you also pass utf-8 encoded characters to your flash or flex application.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.boulevart.be/index.php/2010/10/11/php-and-utf-8-the-way-to-success/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
