<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://www.symforc.com/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Symfony Resources Central - database</title>
  <link>http://www.symforc.com/</link>
  <description>A place that groups many original symfony resources, along with my personal experiments of this great PHP5 MVC framework.</description>
  <language>en</language>
  <pubDate>Sat, 26 Jul 2008 19:34:33 +0200</pubDate>
  <copyright>All rights reserved</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Foreign key's onDelete for dummies</title>
    <link>http://www.symforc.com/post/2008/07/10/Foreign-keys-onDelete-for-dummies</link>
    <guid isPermaLink="false">urn:md5:2e49ed06a0bba3a3d2459d21260ff73d</guid>
    <pubDate>Thu, 10 Jul 2008 23:55:00 +0200</pubDate>
    <dc:creator>Romain Dorgueil</dc:creator>
        <category>database</category>
        <category>database</category><category>doctrine</category><category>model</category><category>orm</category><category>propel</category>    
    <description>&lt;p&gt;Any modern relational database engine supports the onDelete and onUpdate attributes on foreign keys. Propel and Doctrine of
course allows to define it in the schema file (yaml or xml) and I believe it is important to explicitly set it.&lt;/p&gt;    &lt;h2&gt;Restrict mode&lt;/h2&gt;


&lt;p&gt;«Better do nothing than something stupid.»&lt;/p&gt;


&lt;p&gt;The default behaviour is &lt;em&gt;ON DELETE RESTRICT&lt;/em&gt;. This is the most secure setting, so it is normal to have it as default.&lt;/p&gt;


&lt;p&gt;If we try to &lt;em&gt;DELETE&lt;/em&gt; a record referenced by the current object, the database engine just raise an SQL error. If it was not
an error to &lt;em&gt;DELETE&lt;/em&gt; this, you will have to start by removing the linked object, or setting the foreign key value to something different.&lt;/p&gt;


&lt;h2&gt;Cascade mode&lt;/h2&gt;


&lt;p&gt;«No need to keep obsolete things.»&lt;/p&gt;


&lt;p&gt;But our database engines are able to do better. For example, if you have an user's settings table and you delete an user, you may not wan't
to keep his settings. You just set &lt;em&gt;ON DELETE CASCADE&lt;/em&gt; on user's setting foreign key column referencing the user table, and if a user is ever deleted, all his associated properties will be destroyed.&lt;/p&gt;


&lt;p&gt;Along with &lt;em&gt;ON DELETE CASCADE&lt;/em&gt;, we usually add a NOT NULL constraint (or required: true), as the existence of current object is directly
related to the existence of linked object. This may be wrong from time to time, but it is usually the case.&lt;/p&gt;


&lt;h2&gt;Set null mode&lt;/h2&gt;


&lt;p&gt;«We don't rely on this. Just forget it.»&lt;/p&gt;


&lt;p&gt;The last behavior is the &lt;em&gt;ON DELETE SET NULL&lt;/em&gt; option. It does exactly what it says. If linked object is removed, we set our foreign key
value to null.&lt;/p&gt;


&lt;p&gt;This the behavior you may want to use for articles having a link to their author for example. If the author is removed from database,
we don't want to delete his articles, so we just loose the author information (well ok, this is a simplification, but it basically shows
the idea).&lt;/p&gt;


&lt;h2&gt;Why I think we should explicitly use it&lt;/h2&gt;


&lt;p&gt;The default database engine behavior is «I don't know how to handle it». Setting it will add power to your database model, while taking
profit of your RDBMS features. Not setting it is like saying «I don't know which way I want my database to behave in case we delete a linked
object», and that's like saying «I don't know how I want my model to react». Thinking a little to understand which way you want it to work
will make you certain you're understanding the behavior you need in your project.&lt;/p&gt;


&lt;h2&gt;How to set it in your favorite ORM:&lt;/h2&gt;


&lt;h3&gt;schema.yml with Doctrine&lt;/h3&gt;

&lt;pre&gt;
Article:
  columns:
    title:   string(255)
    body:    string(4096)
    user_id: integer(4)
  relations:
    User:
      onDelete: CASCADE
&lt;/pre&gt;


&lt;h3&gt;PHP model with Doctrine&lt;/h3&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;public &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; setUp&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;hasOne&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'User'&lt;/span&gt;, &lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/array&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'local'&lt;/span&gt;&amp;nbsp; &amp;nbsp; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'user_id'&lt;/span&gt;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;'foreign'&lt;/span&gt;&amp;nbsp; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'id'&lt;/span&gt;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;'onDelete'&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'CASCADE'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;


&lt;h3&gt;schema.yml with Propel&lt;/h3&gt;

&lt;pre&gt;
    data_id:  { type: integer, primaryKey: true, foreignTable: data, foreignReference: id, onDelete: cascade, onUpdate: cascade }
&lt;/pre&gt;


&lt;h3&gt;schema.xml with Propel&lt;/h3&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;xml&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;foreign&lt;/span&gt;-key &lt;span style=&quot;color: #000066;&quot;&gt;foreignTable&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;data&amp;quot;&lt;/span&gt; onDelete = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;setnull&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;reference&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;local&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;data_id&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;foreign&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;id&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/foreign&lt;/span&gt;-key&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;</description>
    
    
    
          <comments>http://www.symforc.com/post/2008/07/10/Foreign-keys-onDelete-for-dummies#comment-form</comments>
      <wfw:comment>http://www.symforc.com/post/2008/07/10/Foreign-keys-onDelete-for-dummies#comment-form</wfw:comment>
      <wfw:commentRss>http://www.symforc.com/feed/rss2/comments/77</wfw:commentRss>
      </item>
    
  <item>
    <title>Complex relations population in propel</title>
    <link>http://www.symforc.com/post/2008/01/31/Complex-relations-population-in-propel</link>
    <guid isPermaLink="false">urn:md5:e7d99dbbc10d08b0818277af69fe8c44</guid>
    <pubDate>Thu, 31 Jan 2008 23:47:00 +0100</pubDate>
    <dc:creator>Romain Dorgueil</dc:creator>
        <category>plugin</category>
        <category>database</category><category>dry</category><category>orm</category><category>performance</category><category>plugin</category><category>propel</category><category>symfony</category>    
    <description>    &lt;p&gt;Since quite a bit, I've been faced with an annoying problem on every projects I use propel on. Propel builders only generates some specific cases selection methods, which consists of pretty ugly copy paste of the same code to populate the objects, and if your needs are not satisfied by the finite little number of propel handled cases, you'll have to either use pure SQL, or write a custom doSelect method. That seems okay at first sight, but it is not. In fact, you're about copypasting the propel generated method, and that's a rude violation of D.R.Y. principle.&lt;/p&gt;


&lt;p&gt;I found no solutions during the two last years, but maybe things will change soon with the new sfPropelImpersonatorPlugin. This plugin is aiming at doing arbitrary object population based on informations provided by propel's introspection methods (DatabaseMap/TableMap/ColumnMap) to link populated objects.&lt;/p&gt;


&lt;p&gt;The plugin is currently in very early stage, but is working pretty well for my needs, and I'm looking forward to know what others are thinking about it.&lt;/p&gt;


&lt;p&gt;Related links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.dakrazy.net/document/sf_propel_impersonator_documentation.html&quot; hreflang=&quot;en&quot;&gt;sfPropelImpersonatorPlugin documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://trac.symfony-project.com/wiki/sfPropelImpersonatorPlugin&quot; hreflang=&quot;en&quot;&gt;sfPropelImpersonatorPlugin README&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://trac.symfony-project.com/browser/plugins/sfPropelImpersonatorPlugin/trunk&quot; hreflang=&quot;php&quot;&gt;Browse the source&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
    
    
    
          <comments>http://www.symforc.com/post/2008/01/31/Complex-relations-population-in-propel#comment-form</comments>
      <wfw:comment>http://www.symforc.com/post/2008/01/31/Complex-relations-population-in-propel#comment-form</wfw:comment>
      <wfw:commentRss>http://www.symforc.com/feed/rss2/comments/65</wfw:commentRss>
      </item>
    
  <item>
    <title>Using DBMS functions with sfDoctrine</title>
    <link>http://www.symforc.com/post/2007/10/28/Using-aggregate-functions-with-sfDoctrine</link>
    <guid isPermaLink="false">urn:md5:fc9ef2b2d0bb0c63d95b7e7a5ba34427</guid>
    <pubDate>Tue, 30 Oct 2007 22:11:00 +0100</pubDate>
    <dc:creator>Romain</dc:creator>
        <category>how-to</category>
        <category>aggregate</category><category>database</category><category>doctrine</category><category>MySQL</category><category>portability</category><category>sql</category>    
    <description>&lt;p&gt;I recently had a peek on symfony forum, and seen someone asking &quot;How can I make a SELECT count(*) FROM .... with doctrine?&quot;. An answer to such a question should be pretty obvious as it's of everyday use, but the question seems to have come to life many times as well on IRC than on the forum/mailing list. Here is my two-cents how-to.&lt;/p&gt;    &lt;h2&gt;Using builtin DQL aggregate functions&lt;/h2&gt;


&lt;p&gt;Basically, you can add aggregate functions (functions that operates on a group of records instead of one record, also called GROUP BY functions) to your DQL query the same way you'd do in SQL, provided the fact you're using one of the builtin DQL aggregate functions (COUNT, MAX, MIN, AVG, SUM).&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt; = Doctrine_Query::&lt;span style=&quot;color: #006600;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'COUNT(t.id) cnt'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Table t'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getFirst&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/echo&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'cnt'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;


&lt;h2&gt;Using DBMS specific aggregate functions&lt;/h2&gt;


&lt;p&gt;If the function you need is in the list, no need to go further. But some functions are DBMS specific, for example MySQL provides a &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat&quot; hreflang=&quot;en&quot;&gt;GROUP_CONCAT()&lt;/a&gt; function. It would be nonsense for DQL to provide that, as it would not be translatable in some of the other DBMS SQL, but it would also be nonsense to restrict functionalities to the smallest common functionnalities set of every DBMS that Doctrine supports.&lt;/p&gt;


&lt;p&gt;The way to go is to change the &lt;a href=&quot;http://phpdoctrine.net/index.php/documentation/manual?chapter=configuration#general-attributes:portability:portability-mode-constants&quot; hreflang=&quot;en&quot;&gt;portablility level&lt;/a&gt; of doctrine, to match your needs. As the name implies, you're loosing in portability between the different DBMS, but you're gaining specific functionnalities of yours.&lt;/p&gt;


&lt;p&gt;The following code, running in PORTABILITY_ALL mode, will throw a Doctrine_Query_Exception:&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;Doctrine_Manager::&lt;span style=&quot;color: #006600;&quot;&gt;getInstance&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setAttribute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;ATTR_PORTABILITY&lt;/span&gt;, Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;PORTABILITY_ALL&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt; = Doctrine_Query::&lt;span style=&quot;color: #006600;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'GROUP_CONCAT(t.value) concatedstring'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Table t'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getFirst&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;


&lt;p&gt;Now remove the Doctrine::PORTABILITY_EXPR bit to this attribute, and you will get the correct result (with mySQL):&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;Doctrine_Manager::&lt;span style=&quot;color: #006600;&quot;&gt;getInstance&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setAttribute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;ATTR_PORTABILITY&lt;/span&gt;, Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;PORTABILITY_ALL&lt;/span&gt; ^ Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;PORTABILITY_EXPR&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt; = Doctrine_Query::&lt;span style=&quot;color: #006600;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'GROUP_CONCAT(t.value) concatedstring'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Table t'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getFirst&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/echo&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'concatedstring'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;



&lt;h2&gt;Extending to other functions&lt;/h2&gt;


&lt;p&gt;Ok aggregate functions are usefull, but other functions can be very handy too, &lt;a href=&quot;http://phpdoctrine.net/index.php/documentation/manual?chapter=dql-doctrine-query-language#functional-expressions:string-functions&quot; hreflang=&quot;en&quot;&gt;string functions&lt;/a&gt; for example. Doctrine manual tells us that builtin DQL string functions are CONCAT, SUBSTRING, TRIM, LOWER, UPPER, LOCATE and LENGTH.&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;Doctrine_Manager::&lt;span style=&quot;color: #006600;&quot;&gt;getInstance&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setAttribute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;ATTR_PORTABILITY&lt;/span&gt;, Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;PORTABILITY_ALL&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;$results&lt;/span&gt; = Doctrine_Query::&lt;span style=&quot;color: #006600;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'LENGTH(t.value) val_len, t.value'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Test t'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$results&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/echo&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'value'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; . &lt;span style=&quot;color: #ff0000;&quot;&gt;' =&amp;gt; '&lt;/span&gt; .&lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'val_len'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; .&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;


&lt;p&gt;This code will display all records' value fields, followed by their character length computed by the DBMS, in PORTABILITY_ALL mode.&lt;/p&gt;


&lt;p&gt;Now let's say you want to use &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_bit-length&quot; hreflang=&quot;en&quot;&gt;BIT_LENGTH&lt;/a&gt; MySQL function...&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;Doctrine_Manager::&lt;span style=&quot;color: #006600;&quot;&gt;getInstance&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setAttribute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;ATTR_PORTABILITY&lt;/span&gt;, Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;PORTABILITY_ALL&lt;/span&gt; ^ Doctrine::&lt;span style=&quot;color: #006600;&quot;&gt;PORTABILITY_EXPR&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;$results&lt;/span&gt; = Doctrine_Query::&lt;span style=&quot;color: #006600;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'BIT_LENGTH(t.value) val_len, t.value'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Test t'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$results&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/echo&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'value'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; . &lt;span style=&quot;color: #ff0000;&quot;&gt;' =&amp;gt; '&lt;/span&gt; .&lt;span style=&quot;color: #0000ff;&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'val_len'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; .&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;


&lt;p&gt;This is the way to go again.&lt;/p&gt;


&lt;p&gt;No need to say, this extends to every type of functions available in the different DBMS.&lt;/p&gt;</description>
    
    
    
          <comments>http://www.symforc.com/post/2007/10/28/Using-aggregate-functions-with-sfDoctrine#comment-form</comments>
      <wfw:comment>http://www.symforc.com/post/2007/10/28/Using-aggregate-functions-with-sfDoctrine#comment-form</wfw:comment>
      <wfw:commentRss>http://www.symforc.com/feed/rss2/comments/55</wfw:commentRss>
      </item>
    
</channel>
</rss>