<?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>Symfony e Internet &#187; Propel</title>
	<atom:link href="http://www.dcastello.com/category/propel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dcastello.com</link>
	<description>Conociendo la web</description>
	<lastBuildDate>Mon, 11 Jul 2011 16:03:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Agregar campos virtuales a tu modelo para mejor el rendimiento de symfony</title>
		<link>http://www.dcastello.com/13/03/2010/agregar-campos-virtuales-a-tu-modelo-para-mejor-el-rendimiento-de-symfony/</link>
		<comments>http://www.dcastello.com/13/03/2010/agregar-campos-virtuales-a-tu-modelo-para-mejor-el-rendimiento-de-symfony/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 14:19:55 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Performance]]></category>
		<category><![CDATA[Propel]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[pooling]]></category>

		<guid isPermaLink="false">http://www.dcastello.com/?p=30</guid>
		<description><![CDATA[Hace unos días estaba leyendo el post de Javi (@loalf) sobre como agregar campos virtuales en el AdminGenerator de Symfony. A partir de ahí se originó un interesante conversación con Javi sobre como hacer lo que él planteaba en el post pero yendo un paso más. Ambos teníamos la necesidad de agregar campos virtuales a [...]]]></description>
			<content:encoded><![CDATA[<p>Hace unos días estaba leyendo el post de Javi (@loalf) sobre <a href="http://www.loalf.com/2010/03/ordenar-por-campos-virtuales-con-el-admingenerator-de-symfony/">como agregar campos virtuales en el AdminGenerator de Symfony</a>. A partir de ahí se originó un interesante conversación con Javi sobre como hacer lo que él planteaba en el post pero yendo un paso más. Ambos teníamos la necesidad de agregar campos virtuales a nuestro modelo de datos para almacenar en ellos el resultado de llamar a funciones de nuestro modelo.</p>
<p>Vamos a plantear el tema con un ejemplo. Imaginemos un proyecto simple con symfony bajo <a href="http://propel.phpdb.org/" title="web oficial de Propel">el orm propel</a>, en él queremos guardar información de platos junto con los alimentos que lo componen.</p>
<p><span id="more-30"></span></p>
<p>El esquema sería el siguiente:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;plato&quot;</span> <span style="color: #000066;">phpName</span>=<span style="color: #ff0000;">&quot;Plato&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">primaryKey</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">autoIncrement</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;nombre&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;varchar&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;255&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;slug&quot;</span>   <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;varchar&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;250&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;duracion&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;varchar&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;coste&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;double&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">default</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;comensales&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;receta&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;longvarchar&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;observaciones&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;longvarchar&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;created_at&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;timestamp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;updated_at&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;timestamp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;alimento&quot;</span> <span style="color: #000066;">phpName</span>=<span style="color: #ff0000;">&quot;Alimento&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">primaryKey</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">autoIncrement</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;slug&quot;</span>  <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;varchar&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;250&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;nombre&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;varchar&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;255&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;observaciones&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;longvarchar&quot;</span>  <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;energia&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;double&quot;</span>  <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;agua&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;double&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;etanol&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;double&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;glucidos_totales&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;double&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;polisacaridos&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;double&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;azucares&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;double&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;created_at&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;timestamp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;updated_at&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;timestamp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;unique<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;unique-column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;nombre&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/unique<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;plato_alimento&quot;</span> <span style="color: #000066;">phpName</span>=<span style="color: #ff0000;">&quot;PlatoAlimento&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;plato_id&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">primaryKey</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;alimento_id&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">primaryKey</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cantidad&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">default</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foreign-key</span> <span style="color: #000066;">foreignTable</span>=<span style="color: #ff0000;">&quot;plato&quot;</span> <span style="color: #000066;">onDelete</span>=<span style="color: #ff0000;">&quot;cascade&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;reference</span> <span style="color: #000066;">local</span>=<span style="color: #ff0000;">&quot;plato_id&quot;</span> <span style="color: #000066;">foreign</span>=<span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foreign-key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foreign-key</span> <span style="color: #000066;">foreignTable</span>=<span style="color: #ff0000;">&quot;alimento&quot;</span> <span style="color: #000066;">onDelete</span>=<span style="color: #ff0000;">&quot;cascade&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;reference</span> <span style="color: #000066;">local</span>=<span style="color: #ff0000;">&quot;alimento_id&quot;</span> <span style="color: #000066;">foreign</span>=<span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">onDelete</span>=<span style="color: #ff0000;">&quot;cascade&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foreign-key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Ahora imaginaros el backend de symfony con las acciones típicas de insertar, editar, borrar plato y listar los platos.<br />
Nuestro cliente nos plantea la necesidad de incluir en el listado de platos un dato más como son las kilocalorías que tiene cada plato. Nosotros muy amablemente obecedemos a sus necesidades y nos disponemos a agregar un método a nuestro objeto Plato que realice dicho cálculo acorde a los alimentos que tenga el plato.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">class</span> Plato <span style="color: #000000; font-weight: bold;">extends</span> BasePlato
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCalculoKilocalorias<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// aqui los cálculos necesarios</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$valor</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// resto de funciones de nuestro objeto</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Luego, actualizamos la vista encargada de visualizar el listado de platos agregando un campo más llamado <strong>kilocalorias</strong>. Veamos como quedarían por un lado la acción del controlador y por otro la vista.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">class</span> platoActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeList<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Permitidme dejar de lado filtros y paginaciones</span>
      <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Criteria<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDescendingOrderByColumn</span><span style="color: #009900;">&#40;</span>PlatoPeer<span style="color: #339933;">::</span><span style="color: #004000;">CREATED_AT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">platos</span> <span style="color: #339933;">=</span> PlatoPeer<span style="color: #339933;">::</span><span style="color: #004000;">doSelect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Y la vista (listSuccess.php)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  &lt;table&gt;
  &lt;tr&gt;&lt;th&gt;Nombre Plato&lt;/th&gt;&lt;th&gt;Kilocalorias&lt;/th&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$platos</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$plato</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;tr&gt;&lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nombre</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCalculoKilocalorias</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;&lt;/tr&gt;
  &lt;/php endforeach; ?&gt;
  &lt;/table&gt;</pre></td></tr></table></div>

<p>Bien! ya tenemos la demanda del cliente satisfecha. Pero como informáticos inquietos que somos nos quedamos observando el código y vemos que la llamada al método <strong>getCalculoKilocalorias</strong> puede fastidiarnos un poco el rendimiento. Para cada plato que mostramos estamos haciendo una llamada extra a <strong>getCalculoKilocalorias</strong> y dependiendo de lo que hagamos dentro de dicho método puede que implique más consultas.<br />
Entonces nos planteamos <em>desnormalizar nuestro esquema de base de datos y agregar un campo más a nuestro objeto Plato llamada kilocalorias</em>. Así pensamos reducir las consultas y acciones a ejecutar en nuestra aplicación de gestión de platos.</p>
<p>Nos ponemos manos a la obra, alteramos el esquema de datos agregando la nueva columna, y vamos felizmente a cambiar la vista.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  &lt;table&gt;
  &lt;tr&gt;&lt;th&gt;Nombre Plato&lt;/th&gt;&lt;th&gt;Kilocalorias&lt;/th&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$platos</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$plato</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;tr&gt;&lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nombre</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;!-- Fijaos que ahora estamos mostrando la nueva columna y no llamando al método getCalculoKilocalorias --&gt;
    &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getKilocalorias</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;&lt;/tr&gt;
  &lt;/php endforeach; ?&gt;
  &lt;/table&gt;</pre></td></tr></table></div>

<p>Ahora nos surge una serie de dudas ¿cual es el lugar más adecuado para ir actualizando nuestro campo virtual?. Para ello veamos cual es el proceso de guardado de un plato. Recordaos que plato mantiene una relación N-N con alimentos, por lo que los alimentos del plato son guardados en una tabla intermedia plato_alimento.</p>
<ol>
<li>Mostramos el form de platos en el backend</li>
<li>El usuario agrega y/o quita alimentos al plato</li>
<li>Llamamos al método <strong>save</strong> del objeto PlatoForm</li>
<li>El método save se encarga de guardar tanto los datos del plato como los alimentos que lo componen</li>
<li>Finalmente devolvemos el control al usuario con los cambios guardados</li>
</ol>
<p>Veamos con más detalle el proceso:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">class</span> PlatoForm <span style="color: #000000; font-weight: bold;">extends</span> BasePlatoForm
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// aqui hay más codigo</span>
      try
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$con</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// LLAMAMOS A doSave</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSave</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$con</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$con</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rollBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">throw</span> <span style="color: #000088;">$e</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> doSave<span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// ...</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// AQUÍ GUARDAMOS EL OBJETO PLATO</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Y con saveEmbeddedForm nos encargamos de guardar los alimentos</span>
      <span style="color: #666666; font-style: italic;">// del plato en la tabla PlatoAlimentos a través del objeto PlatoAlimento</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveEmbeddedForms</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Visto el proceso de guardado de un form con symfony (muy resumido) volvemos preguntarnos donde actualizar nuestro campo virtual ¿en el postSave del objeto Plato o en el postSave del objeto PlatoAlimento?.<br />
Los postSave, preSave, postInsert, preInsert, etc, <a href="http://propel.phpdb.org/trac/wiki/Users/Documentation/1.4/WhatsNew" title="Explicación con más detalle en la web oficial de Propel">son hooks que facilita propel</a> para introducir código antes y/o después de cada acción.<br />
Pienso que lo más adecuado es hacerlo en el postSave de PlatoAlimento ya que el valor de nuestro campo virtual depende siempre del contenido existente en la tabla plato_alimento. De ahí que cualquier cambio que se produzca en dicha tabla, independientemente de dónde y quién lo este produciendo, provoca automáticamente que nuestro campo virtual sea actualizado. De este modo el campo virtual del objeto Plato, siempre tiene un valor fiable en dicha columna.<br />
Bien, ya sabemos quién debe disparar la &#8220;alerta&#8221; para cambiar nuestro campo virtual, veamos el código</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">class</span> PlatoAlimento <span style="color: #000000; font-weight: bold;">extends</span> BasePlatoAlimento
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postSave<span style="color: #009900;">&#40;</span>PropelPDO <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span>PlatoAlimentoPeer<span style="color: #339933;">::</span><span style="color: #004000;">DATABASE_NAME</span><span style="color: #339933;">,</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">CONNECTION_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      parent<span style="color: #339933;">::</span><span style="color: #004000;">postSave</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$plato</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPlato</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Método encargado de resetear el campo virtual</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cambiaColumnaVirtual</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// No solo tras insertar/actualizar debemos dar la alarma, cuando borremos también</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postDelete<span style="color: #009900;">&#40;</span>PropelPDO <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span>PlatoAlimentoPeer<span style="color: #339933;">::</span><span style="color: #004000;">DATABASE_NAME</span><span style="color: #339933;">,</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">CONNECTION_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000088;">$plato</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPlato</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Método encargado de resetear el campo virtual</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cambiaColumnaVirtual</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      parent<span style="color: #339933;">::</span><span style="color: #004000;">postDelete</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">class</span> Plato <span style="color: #000000; font-weight: bold;">extends</span> BasePlato
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> cambiaColumnaVirtual<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;">setKilocalorias</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CalculoKilocalorias</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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> getCalculoKilocalorias<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// aqui los cálculos necesarios</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$valor</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Hasta ahora hemos visto porqué agregar un campo virtual en symfony, cómo agregarlo y dónde. Pero hay una cosa más, el código de anterior tiene un problema.<br />
Si lo ejecutásemos, <em>nuestro objeto Plato no tendría actualizado el campo virtual cuando agregásemos un alimento nuevo al plato</em>. ¿Porqué? Veámoslo.</p>
<p>Nuestro proyecto symfony está utilizando propel en su versión 1.4, y propel a partir de su versión 1.3 incorpora <a href="http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/Upgrading" title="Más información en la web de propel">object instace pooling</a>, es decir estamos &#8220;cacheando&#8221; objetos.<br />
Dicho comportamiento está indicado en el fichero <strong>database.yml</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">all:
  propel:
    class: sfPropelDatabase
    param:
      dsn:        mysql:dbname=miBBDD;host=localhost
      username:   mi_user
      password:   mi_clave
      encoding:   utf8
      persistent: true
      pooling:    true // Aqui está el parámetro
      classname:  PropelPDO</pre></div></div>

<p><strong>¿En qué o dónde nos afecta esto a la solución que habíamos planteado?</strong>.<br />
Si os fijáis en el código del método postSave del objeto PlatoAlimento, hemos recuperado el objeto Plato ($this->getPlato()), y posteriormente hemos hemos actualizado nuestro campo virtual y ahí es donde está el problema, ya que el objeto Plato recuperado mantiene los datos &#8220;viejos&#8221; de los alimentos y todavía no se ha dado cuenta de nuestros cambios. Según esto todo el proceso de cálculo de las kilocalorías devolverá los mismos valores que teníamos.<br />
<strong>¿Cómo lo solucionamos?</strong><br />
Todos los objetos de <a href="http://www.symfony-project.org/">Symfony</a> disponen de unos métodos para limpiar y obtener objetos del &#8220;pool&#8221;. Y uno de esos métodos es <strong>reload</strong> que recibe dos parámetros.<br />
El primero es para indicar si hacemos el &#8220;reload&#8221; en profundidad, es decir recargando todos los objetos relacionados que puede tener. En nuestro caso nos interesa que relea todo lo relacionado con alimentos (PlatoAlimento). El segundo parámetro es el link de conexión a la base de datos ($con).</p>
<p>Mostremos el código final</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">class</span> PlatoAlimento <span style="color: #000000; font-weight: bold;">extends</span> BasePlatoAlimento
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postSave<span style="color: #009900;">&#40;</span>PropelPDO <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span>PlatoAlimentoPeer<span style="color: #339933;">::</span><span style="color: #004000;">DATABASE_NAME</span><span style="color: #339933;">,</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">CONNECTION_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      parent<span style="color: #339933;">::</span><span style="color: #004000;">postSave</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$plato</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPlato</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// releemos los valores de plato así como de sus objetos relacionados</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reload</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Método encargado de resetear el campo virtual</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cambiaColumnaVirtual</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// No solo tras insertar/actualizar debemos dar la alarma, cuando borremos también</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postDelete<span style="color: #009900;">&#40;</span>PropelPDO <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span>PlatoAlimentoPeer<span style="color: #339933;">::</span><span style="color: #004000;">DATABASE_NAME</span><span style="color: #339933;">,</span> Propel<span style="color: #339933;">::</span><span style="color: #004000;">CONNECTION_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000088;">$plato</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPlato</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// releemos los valores de plato así como de sus objetos relacionados</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reload</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Método encargado de resetear el campo virtual</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cambiaColumnaVirtual</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$plato</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      parent<span style="color: #339933;">::</span><span style="color: #004000;">postDelete</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">class</span> Plato <span style="color: #000000; font-weight: bold;">extends</span> BasePlato
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> cambiaColumnaVirtual<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;">setKilocalorias</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CalculoKilocalorias</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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> getCalculoKilocalorias<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// aqui los cálculos necesarios</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$valor</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Eso es todo! Ya hemos visto como mejorar un poco nuestras aplicaciones symfony. Todo lo expuesto aquí esta abierto a debate y mejoras, así que no dudéis en comentarlas.<br />
Un cosa más, tened en cuenta que nuestra aplicación web para la gestión de platos las consultas de insert/update van a ser mucho menores que las de selección. Esto es un factor a tener en cuenta a la hora de buscar mejorar el rendimiento. Si las acciones de inserción/borrado/actualización fuesen mayores habría que plantearse otra estrategia de optimización.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dcastello.com/13/03/2010/agregar-campos-virtuales-a-tu-modelo-para-mejor-el-rendimiento-de-symfony/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

