<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eat my monkeydust &#187; Doctrine</title>
	<atom:link href="http://eatmymonkeydust.com/category/doctrine/feed/" rel="self" type="application/rss+xml" />
	<link>http://eatmymonkeydust.com</link>
	<description>Life, PHP and Symfony - straight from the monkey's mouth</description>
	<lastBuildDate>Fri, 07 Oct 2011 14:00:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Redpill Linpro and Varnish software, Gold sponsors of Symfony Live (Paris) 2011</title>
		<link>http://eatmymonkeydust.com/2011/01/redpill-linpro-and-varnish-software-gold-sponsors-of-symfony-live-paris-2011/</link>
		<comments>http://eatmymonkeydust.com/2011/01/redpill-linpro-and-varnish-software-gold-sponsors-of-symfony-live-paris-2011/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 11:26:18 +0000</pubDate>
		<dc:creator>Russ</dc:creator>
				<category><![CDATA[conferences]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://eatmymonkeydust.com/?p=284</guid>
		<description><![CDATA[Introduction As a Symfony user and contributor for several years, it gives me great pleasure to announce that the company I work for Redpill Linpro along with sister company Varnish Software have agreed to be gold sponsors of the Symfony Live conference in Paris (March 2011). As a company we have benefited from the Symfony [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://eatmymonkeydust.com/wp-content/uploads/2011/01/redpill_linpro_varnish.png"><img class="size-full wp-image-285 alignright" style="border: 0pt none;" title="redpill_linpro_varnish" src="http://eatmymonkeydust.com/wp-content/uploads/2011/01/redpill_linpro_varnish.png" alt="" width="231" height="153" /></a></p>
<h2>Introduction</h2>
<p>As a Symfony user and contributor for several years, it gives me great pleasure to announce that the company I work for <a href="http://redpill-linpro.com" target="_blank">Redpill Linpro</a> along with sister company <a href="http://varnish-software.com" target="_blank">Varnish Software</a> have agreed to <strong><span style="color: #ff6600;">be gold sponsors</span></strong> of the <a href="http://www.symfony-live.com/paris" target="_blank">Symfony Live conference in Paris</a> (March 2011).</p>
<p>As a company we have benefited from the Symfony framework in many ways including:</p>
<ul>
<li>Rapid development</li>
<li>Consistency (easy to bring team members in and out)</li>
<li>Community support</li>
<li>Great documentation</li>
</ul>
<p>And given back by providing time for our developers to:</p>
<ul>
<li>File bug reports</li>
<li>Submit patches</li>
<li>Develop plugins</li>
<li>Help other users (particularly in IRC and the Symfony Forum)</li>
<li>Update the documentation</li>
</ul>
<p>By agreeing to <strong><span style="color: #ff6600;">gold sponsorship</span></strong> we are now also making a small financial contribution to the project, whilst at the same time giving ourselves a chance to become more known in the Symfony world, especially with <a href="http://symfony-reloaded.org/" target="_blank">Symfony2</a> just around the corner and its great support for Varnish!</p>
<h2>Brief history of the companies</h2>
<p>Until recently, Redpill and Linpro were two separate companies, and the work done by Varnish Software was a part of Linpro. In 2008, Linpro and Redpill merged to form Redpill Linpro, and in 2010 Varnish Software broke away to form a &#8220;daughter&#8221; company and concentrate on the Varnish cache. Confused?</p>
<p>The most important thing is that all of the companies involved are dedicated to open source, you can read more about <a href="http://redpill-linpro.com/About-us" target="_blank">Redpill Linpro</a> and <a href="http://www.varnish-software.com/about" target="_blank">Varnish Software</a> on their respective web sites.</p>
<h2>Employment Opportunities for Symfony developers in &#8220;the North&#8221;</h2>
<p>If you are looking for a company to work for with a strong PHP team, and either already live in or are considering a move to &#8220;the Nordic region¹&#8221; please get in touch. We also have opportunities for Java developers and <a href="http://redpill-linpro.com/Grow-with-us/Who-are-we-looking-for" target="_blank">various other posts</a> related to Open Source technologies, and if we don&#8217;t have a specific post for you but you are a skilled open source enthusiast, we are also interested in your CV &#8211; who knows, we might be able to create one for you.</p>
<p>¹We have offices in Norway, Sweden, Denmark and Finland, the bulk of Symfony operations are presently in Oslo.</p>
]]></content:encoded>
			<wfw:commentRss>http://eatmymonkeydust.com/2011/01/redpill-linpro-and-varnish-software-gold-sponsors-of-symfony-live-paris-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Doctrine 2 with symfony 1.x [Part 1]</title>
		<link>http://eatmymonkeydust.com/2010/03/using-doctrine-2-with-symfony-1-x-part1/</link>
		<comments>http://eatmymonkeydust.com/2010/03/using-doctrine-2-with-symfony-1-x-part1/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 23:13:13 +0000</pubDate>
		<dc:creator>Russ</dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Doctrine 2]]></category>
		<category><![CDATA[sfDoctrine2plugin]]></category>

		<guid isPermaLink="false">http://eatmymonkeydust.com/?p=256</guid>
		<description><![CDATA[Background It&#8217;s been a while since Jonathan announced the availability of Doctrine 2 for Symfony 1.x (wouldn&#8217;t recommend trying it with anything less than 1.3) and a a few things have changed since then, so here&#8217;s a refresher. We are at a crossroads now with Symfony 2 looming on the horizon, and many developers may [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>It&#8217;s been a while since <a href="http://www.jwage.com/" target="_blank">Jonathan</a> announced the availability of <a href="http://www.symfony-project.org/blog/2009/10/05/symfony-and-doctrine-2" target="_blank">Doctrine 2 for Symfony 1.x</a> (wouldn&#8217;t recommend trying it with anything less than 1.3) and a a few things have changed since then, so here&#8217;s a refresher.</p>
<p>We are at a crossroads now with Symfony 2 looming on the horizon, and many developers may wish to wait until that is more stable (later this year) to make the move to Symfony 2 and Doctrine 2 simultaneously. This is not such a bad idea, however Doctrine 2 is already at a level where you may wish to consider using it in your projects, you won&#8217;t be disappointed if you do!</p>
<p>It will take me a while to explain why Doctrine 2 is better than Doctrine 1, and more importantly why you should start using &#8211; but you can just take my word for it and take a look at a couple of <a href="http://www.slideshare.net/search/slideshow?searchfrom=header&amp;q=doctrine+2" target="_blank">Jonathan&#8217;s presentations</a> to back up my claim. (I Recommend <a href="http://www.slideshare.net/jwage/doctrine-2-not-the-same-old-php-orm" target="_blank">Doctrine 2 &#8211; not the same old PHP ORM</a>)</p>
<h2>Getting started</h2>
<p>Check out the plugin and set up the database as described in <a href="http://www.symfony-project.org/blog/2009/10/05/symfony-and-doctrine-2">Jon&#8217;s blog</a>, but don&#8217;t configure the schema just yet <img src='http://eatmymonkeydust.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>We&#8217;re going to go all out with the &#8220;Doctrine 2&#8243; way of doing things, so we&#8217;ll be using annotations, not yaml or xml &#8211; although you can look up that syntax if you prefer. (A lot of the stuff below won&#8217;t work though &#8211; you can&#8217;t have multiple yaml files for example yet).</p>
<p>In your project configuration class, you will have access to some methods:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configureDoctrineConnection<span style="color: #009900;">&#40;</span>\Doctrine\ORM\Configuration <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configureEntityManager<span style="color: #009900;">&#40;</span>\Doctrine\ORM\EntityManager <span style="color: #000088;">$em</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the first of these, you need to add all the things to configure Doctrine before the entity manager can be created. Once Doctrine has created the entity manager, you can use the second method if you need to, to further configure the entity manager. (Registering a Doctrine listener for example).</p>
<p>So, in your configureDoctrineConnection() method, you&#8217;ll want to do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Decide where you want the proxy classes to be stored, and which namespace they should use</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProxyDir</span><span style="color: #009900;">&#40;</span>sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sf_cache_dir&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/Proxies&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProxyNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// You may want to make this environment specific for performance reasons</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAutoGenerateProxyClasses</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSqlLogger</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This will get things working, but later you will want to use APC</span>
<span style="color: #666666; font-style: italic;">// or another &quot;real&quot; cache for production</span>
<span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\Cache\ArrayCache<span style="color: #339933;">;</span>
<span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\Annotations\AnnotationReader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMetadataCacheImpl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setQueryCacheImpl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Tell Doctrine where to find your entities (you may have more than one location)</span>
<span style="color: #666666; font-style: italic;">// This is mostly required for cli tasks that iterate over all of your entities</span>
<span style="color: #000088;">$paths</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sf_lib_dir&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/Entities&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Populate this with all the locations of your entities</span>
<span style="color: #000088;">$reader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDefaultAnnotationNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Doctrine\ORM\Mapping\\'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$annotation</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\ORM\Mapping\Driver\AnnotationDriver<span style="color: #009900;">&#40;</span><span style="color: #000088;">$reader</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paths</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMetadataDriverImpl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$annotation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register all the classes that Doctrine needs to autoload</span>
<span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\ClassLoader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Entities\doctrine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIncludePath</span><span style="color: #009900;">&#40;</span>sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_lib_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\ClassLoader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIncludePath</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cachedir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\Common\ClassLoader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Another<span style="color: #000099; font-weight: bold;">\N</span>amespace&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIncludePath</span><span style="color: #009900;">&#40;</span>sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_lib_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ok, that was a lot of stuff &#8211; but Doctrine 2 is more verbose &#8211; meaning less magic, but more explicit code. Some of the above calls may not be necessary &#8211; I need to do further testing, and also I&#8217;d like to get a lot of these things &#8220;standardised&#8221; in the plugin, so you can skip a lot of this if you follow a &#8220;default&#8221; path. The problem with the original release of the plugin was that it was not possible to have multiple class dirs, which makes it impossible to have plugins (for example) that also contain Entities. With the approach above, you explicitly add as many classes as you like to the annotation driver and the autoloader, and your plugin configuration classes can do the same.</p>
<h2>Active Entity</h2>
<p>The plugin comes bundled with Active Entity &#8211; this means that by extending ActiveEntity from our model classes (Entities) we can use our classes in much the same way as in Doctrine 1. Things like the following become available:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
\Entities\User<span style="color: #339933;">::</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>ActiveEntity was written and included with good intentions, because for Doctrine 2 to continue to be compatible with symony in the same way as Doctrine 1, a bit of magic needed to be re-introduced. <strong>BUT</strong> &#8211; for anyone that has been excited by Doctrine 2, one of the major breakthroughs is the non-intrusive model, and ActiveEntity kills that (along with a kitten):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//With ActiveEntity:</span>
<span style="color: #000000; font-weight: bold;">class</span> \Entities\User <span style="color: #000000; font-weight: bold;">extends</span>  \DoctrineExtensions\ActiveEntity <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Without ActiveEntity:</span>
<span style="color: #000000; font-weight: bold;">class</span> \Entities\User extends<span style="color: #339933;">-</span>or<span style="color: #339933;">-</span>does<span style="color: #339933;">-</span>not<span style="color: #339933;">-</span>extend  \Whatever\The\Hell\You\Like <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>So I strongly recommend that you <strong>Don&#8217;t</strong> extend ActiveEntity &#8211; and gradually the plugin will be &#8220;fixed&#8221; so that is never necessary. Currently basic object forms and some widgets are working fine without it, but I haven&#8217;t tested it with admin generators or anything like that yet so feedback is appreciated.</p>
<p>If you follow my advice, you have to do everything the &#8220;Doctrine 2 way&#8221;. That means taking control of your own code! Write your own getters and setters, extend your own classes (if you want to) &#8211; be verbose, write OOP code, etc!</p>
<h2>Ongoing development</h2>
<p>Doctrine 2 is still in Alpha at the moment and is constantly changing. Since we are using it actively in a project at the moment, we normally spot the changes pretty quickly and update the plugin to keep up, however we are not using all the aspects of the plugin (like generators, all the form widgets, etc) so from time to time something might completely fail when you svn up.</p>
<p>We are also trying to slowly, and safely (for BC) remove the need for ActiveEntity &#8211; but we&#8217;re being careful with this because we don&#8217;t want to break existing projects that are using it.</p>
<p>Report bugs in the usual way using the <a href="http://trac.symfony-project.org/newticket" target="_blank">symfony bug tracker</a> (if it&#8217;s plugin related) or on <a href="http://www.doctrine-project.org/jira/secure/CreateIssue!default.jspa" target="_blank">Doctrine Jira</a> (if you know it is a Doctrine issue). When reporting plugin bugs, please register the bug under the sfDoctrine component and <strong>add the keyword sfDoctrine2Plugin</strong> as I use a <a href="http://trac.symfony-project.org/query?status=new&amp;status=assigned&amp;status=reopened&amp;keywords=%7EsfDoctrine2Plugin&amp;order=priority" target="_blank">filtered query</a> to keep an eye on Doctrine 2 related issues.</p>
<h2>Coming in part 2&#8230;</h2>
<p>Will either be based on feedback/suggestions (if any) or I&#8217;ll move on to some real world examples and best practices when it comes to integrating symfony and Doctrine 2.</p>
]]></content:encoded>
			<wfw:commentRss>http://eatmymonkeydust.com/2010/03/using-doctrine-2-with-symfony-1-x-part1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Doctrine Gotchas &#8211; delete cascade, SoftDelete and foreignType</title>
		<link>http://eatmymonkeydust.com/2009/05/doctrine-gotchas-delete-cascade-sofdelete-and-foreigntype/</link>
		<comments>http://eatmymonkeydust.com/2009/05/doctrine-gotchas-delete-cascade-sofdelete-and-foreigntype/#comments</comments>
		<pubDate>Sat, 16 May 2009 13:41:43 +0000</pubDate>
		<dc:creator>Russ</dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[cascade]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[foreignType]]></category>
		<category><![CDATA[relations]]></category>
		<category><![CDATA[transitive persistence]]></category>

		<guid isPermaLink="false">http://eatmymonkeydust.com/?p=204</guid>
		<description><![CDATA[These are mistakes anyone can make, as proved by our team of experienced developers who only came across these late into a project &#8211; after developing with Doctrine for over 6 months. Cascading / Transitive persistence In Doctrine there are two ways to define a cascade, one will tell Doctrine what to do, the other [...]]]></description>
			<content:encoded><![CDATA[<p>These are mistakes anyone can make, as proved by our team of experienced developers who only came across these late into a project &#8211; after developing with Doctrine for over 6 months.</p>
<h2>Cascading / Transitive persistence</h2>
<p>In Doctrine there are two ways to define a cascade, one will tell Doctrine what to do, the other defines how that condition is set up on your database when Doctrine builds the sql commands. The reason this was a gotcha for me, is that we only ever used one of the types, and were completely unaware of the other one! The Doctrine manual is quite long, and unless you&#8217;ve read it several times cover to cover it&#8217;s easy to miss these things &#8211; although next time I will at least be reading the appropriate section of the manual, regardless of what I <em>think</em> I already know.</p>
<p>So, you want a delete cascade? You might do something like this in your yaml file:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">PhoneNumber:
  columns:
    user_id:
      type: integer
  relations:
    User:
      class:       User
      local:       user_id
      foreign:     id
      onDelete:    CASCADE</pre></div></div>

<p>What the above definition says is that this phone number is connected to a user, and if the user record is deleted &#8211; in order to maintain database integrity with foreign keys, the corresponding phone number records should also be deleted. This is perfect behaviour &#8211; but Doctrine does not do anything with it, it simply sets up the database to handle it, so if you are using a dbms that supports delete cascades then everything will be handled at the database level.</p>
<h3>Adding softDelete to the equation</h3>
<p>So here comes the gotcha, all of our tables use the softDelete behaviour &#8211; which means that instead of the row being deleted, a flag is set (deleted=true) which then magically makes the row appear deleted in all your queries (Providing you have <a href="http://www.doctrine-project.org/documentation/manual/1_1/en/behaviors:core-behaviors:softdelete">dql callbacks turned on</a>). This means that the row is never actually deleted at the databse level, and thus the cascade is never applied. Now I always knew this, I mean I wouldn&#8217;t install a behaviour before knowing what it does right? What I wrongly assumed was that the call to onDelete: CASCADE would take care of &#8220;soft deleting&#8221; my relations too, but it doesn&#8217;t, in fact that line is effectively useless when combined with softDelete. For that you need to set the doctrine cascade parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">PhoneNumber:
  actAs: [softDelete]
  columns:
    user_id:
      type: integer
  relations:
    User:
      class:       User
      local:       user_id
      foreign:     id
      foreignType: one
      cascade:     [delete]</pre></div></div>

<p>Now the behaviour will be as expected. If you are using softDelete and your application has a lot of delete operations, you should take some time to understand what is happening, because Doctrine must fetch and load all of the related objects and then check their relations and cascade settings and so on, so in a large system deleting a single user could result in quite a lot of database queries and cpu time. It is for this reason that database level cascades are preferable, so maybe being more picky about which models you apply the softDelete behaviour would be prudent. <a href="http://www.doctrine-project.org/documentation/manual/1_1/en/defining-models:transitive-persistence">Learn more about Doctrine Transitive Persistance</a>.</p>
<h2>Defining types of foreign key relations</h2>
<p>This one is also one of those &#8220;read the manual&#8221; situations, but similarly to the above example what caught us out here is that we again thought that there was only one option for defining what we wanted to do, when in fact there were too parameters we should have been considering. This gotcha also only applies if you are defining your relations in a yaml file, as the problem lies in its interpretation when Doctrine builds the base classes.</p>
<p>Look at the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">User:
  columns:
    id:
      type: integer
      notnull: true
  relations:
    Payment:
      class:       Payment
      local:       id
      foreign:     user_id
      foreignType: many</pre></div></div>

<p>The intention was that a user could have multiple payments, and it seemed to make sense to define it using the foreignType parameter above, however when the model was built &#8211; this always resulted in a $this-&gt;hasOne(&#8230;) statement being created, because what was actually happening is that we were not defining the relationship at the other end, we were actually defining it at the local end! So foreignType: many actually means &#8220;Many users can have one payment&#8221; &#8211; hence the interpretation of $this-&gt;hasOne(..)</p>
<p>In actual fact using the foreignType key is mostly useless, as Doctrine &#8220;guesses&#8221; this side of the relationship. It&#8217;s only necessary if Doctrine guesses incorrectly and you need to override it, our example above should actually have been using the &#8220;type&#8221; parameter, which defines the relationship from the other perspective:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">User:
  columns:
    id:
      type: integer
      notnull: true
  relations:
    Payments:
      class:    Payment
      local:    id
      foreign:  user_id
      type:     many</pre></div></div>

<p>Adding [ foreignType: one ] to this schema declaration would have no offect, as Doctrine would guess it to be the case (in this example). The above declaration will now correctly form a $this-&gt;hasMany(&#8230;) statement in your base class. To make it easy to remember, just forget about foreignType all together unless you come across a special case where you nee it &#8211; and prepend the word &#8220;has&#8221; to the type parameter, that should give you a clue as to what you are defining.</p>
<p>So, with one-to-one relationships, the type parameter is unnecessary, with one-to-many you specify the type: many (Doctrine will guess the one part) and with many-to-one you also do not need to specify the type as it will be guessed based on how you&#8217;ve got the relation set up from the other model&#8217;s perspective.</p>
<p><a href="http://www.doctrine-project.org/documentation/manual/1_1/en/defining-models:relationships:foreign-key-associations">Read more about relations in the Doctrine manual.</a></p>
<h2>Conclusion</h2>
<p>it&#8217;s hard to believe that after 6 months of coding a large application that currently has over 30,000 lines of code and growing we could have a schema file with several useless calls to onDelete: CASCADE and even more useless calls to foreignType. It&#8217;s even harder to believe that in the early days of the project, we simply overrode the setup methods to add the relations that we needed, when the Schema.yml file failed to come up with the goods. Next time I think I&#8217;ll spend a little more time with the documentation, although we are <em>of course</em> experts now &#8211; so maybe next time we won&#8217;t need to? <img src='http://eatmymonkeydust.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://eatmymonkeydust.com/2009/05/doctrine-gotchas-delete-cascade-sofdelete-and-foreigntype/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Customising Symfony forms &#8211; be careful with base class inheritence</title>
		<link>http://eatmymonkeydust.com/2009/03/customising-symfony-forms-be-careful-with-base-class-inheritence/</link>
		<comments>http://eatmymonkeydust.com/2009/03/customising-symfony-forms-be-careful-with-base-class-inheritence/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 14:12:25 +0000</pubDate>
		<dc:creator>Russ</dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://eatmymonkeydust.com/?p=92</guid>
		<description><![CDATA[Background Recently I was working on a form for updating a couple of very simple values for a single table. When creating such forms, where we only need a subset of the available columns to be editable, we always have the option of either unsetting the fields we don&#8217;t need, or overriding the widgetSchema. In [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>Recently I was working on a form for updating a couple of very simple values for a single table. When creating such forms, where we only need a subset of the available columns to be editable, we always have the option of either unsetting the fields we don&#8217;t need, or overriding the widgetSchema. In this case, I opted for the latter, since I only needed to edit 2 columns out of a possible 10, I didn&#8217;t think adding 8 fields to the unset() function was the cleanest way. The following examples contain obfuscated data.</p>
<h2>Overriding the widget schema</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setWidgets</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'amount'</span>               <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'reduced_amount'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Labels and decorator stuff here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>My plan was to inherit the validators that already exist in the base class, since they do the job for what I need.</p>
<h2>The error</h2>
<p>The form worked fine for an insert, but when I came to update an existing record, the error was quite strange:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">SQLSTATE<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">23505</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> Unique violation<span style="color: #339933;">:</span> <span style="color: #cc66cc;">7</span> ERROR<span style="color: #339933;">:</span> 
  duplicate <span style="color: #990000;">key</span> value violates unique constraint 
  <span style="color: #0000ff;">&quot;body_fee_version_pkey&quot;</span></pre></div></div>

<p>The problem it seemed was that my versionable behaviour was not incrementing the version value before attempting to insert a new version record. After a long period of debugging the versionable behaviour, along with some of my other custom behaviours, I was no closer to a solution.</p>
<p>I started to dig into the form classes, working backwords through all the object update methods, save, dosave, etc. Until I finally stumbled across this line:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">values</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clean</span><span style="color: #009900;">&#40;</span>
   <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">deepArrayUnion</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">taintedValues</span><span style="color: #339933;">,</span> 
   <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">convertFileInformation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">taintedFiles</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Before this call, everything seemed ok, but after this call, my values array, which at this stage only contained the two fields that had been posted, now suddenly included a value for all the fields in the table. Why? It then occurred to me that the entire validator schema was being processed, not just the fields that are actually posted! This means that all the validators that are required=false will silently return a &#8220;clean&#8221; value, which is most likely the database default.</p>
<p>So what did this mean? Well, it meant that the validator was &#8220;cleaning&#8221; all the columns that had not been submitted with the form, including the version column, which was being set to null. When the versionable behaviour kicked in, it read this null value and incremented by 1 for the next version, which then became 1 &#8211; a version which of course already existed, causing the error.</p>
<h2>The solution</h2>
<p>The solution is blindingly simple, we don&#8217;t just declare the widget schema, we must also declare the validator schema. Whilst this seems like it makes sense, I feel that it is a shame that I have to essentially copy and paste the necessary validators from the base class. The alternative of course would have been to unset the offending fields, but then we are back to option 1 above, unsetting 8 of 10 fields when it seems cleaner just to declare the 2 fields I actially need.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setWidgets</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'amount'</span>               <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'reduced_amount'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Messages declared here as array since they are the same</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setValidators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;amount&quot;</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfNumberValidator<span style="color: #009900;">&#40;</span>
                       <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">&quot;reduced_amount&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfNumberValidator<span style="color: #009900;">&#40;</span>
                       <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
 <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Labels and decorator stuff here</span></pre></div></div>

<h2>Conclusion</h2>
</p>
<p>I have been working with Symfony for over a year and a half, and with Symfony forms since they were born &#8211; and yet I was still caught out by something that seems quite simple, because I assumed it would be ok. There are so many things that could have alerted me to this problem and saved me a lot of time, for example if any of the extra fields had been required=true then at least I would have had some form errors to give me a clue!</p>
<p>I may suggest that the default behaviour should be to only process the validators that match the widgets, or maybe at least for the form to err if extra validators are found that are not used, as I feel that this is a mistake that others could make, and as I mentioned above, it&#8217;s a shame that we have to re-declare validators that are already present in the base class.</p>
]]></content:encoded>
			<wfw:commentRss>http://eatmymonkeydust.com/2009/03/customising-symfony-forms-be-careful-with-base-class-inheritence/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

