<?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>Montes&#039; coding blog</title>
	<atom:link href="http://en.mooontes.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.mooontes.com</link>
	<description>trying to be a better coder</description>
	<lastBuildDate>Fri, 03 Jun 2011 13:32:21 +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>Symfony2 ultra-fast start tutorial</title>
		<link>http://en.mooontes.com/2011/03/27/symfony2-ultra-fast-start-tutorial/</link>
		<comments>http://en.mooontes.com/2011/03/27/symfony2-ultra-fast-start-tutorial/#comments</comments>
		<pubDate>Sun, 27 Mar 2011 17:12:33 +0000</pubDate>
		<dc:creator>montes</dc:creator>
				<category><![CDATA[symfony2]]></category>
		<category><![CDATA[doctrine2]]></category>

		<guid isPermaLink="false">http://en.mooontes.com/?p=7</guid>
		<description><![CDATA[NOTE: This tutorial has been made with Ubuntu 11.04 &#8211; Natty Narwhal, it hasn&#8217;t been tested on other OSs. This is some kind of cheat sheet to help you if you forget something basic about symfony2, but you obviously must read symfony2 documentation to know what&#8217;s all this about http://symfony.com/doc/2.0/book/index.html Source code of this tutorial [...]]]></description>
			<content:encoded><![CDATA[<p><em>NOTE: This tutorial has been made with Ubuntu 11.04 &#8211; Natty Narwhal, it hasn&#8217;t been tested on other OSs. This is some kind of cheat sheet to help you if you forget something basic about symfony2, but you obviously must read symfony2 documentation to know what&#8217;s all this about <a href="http://symfony.com/doc/2.0/book/index.html">http://symfony.com/doc/2.0/book/index.html</a></em></p>
<p>Source code of this tutorial can be downloaded at: <a href='https://github.com/montes/Adictos-Symfony2-Bundle' target='_blank'>https://github.com/montes/Adictos-Symfony2-Bundle</a></p>
<p>UPDATED 5/23/2011: Updated for symfony2 beta2</p>
<p>&nbsp;</p>
<h2>1. Download</h2>
<p>Download Symfony2 Standard Edition (at the moment of writing this, last version is BETA2): <a href='http://symfony.com/download' target='_blank'>http://symfony.com/download</a></p>
<p>Extract</p>
<pre class="brush: bash; title: ; notranslate">
tar -zxvf Symfony_Standard_Vendors_2.0.0BETA2.tgz
</pre>
<p>And change permissions to make <em>app/cache</em> and <em>app/logs</em> writable, for example:</p>
<pre class="brush: bash; title: ; notranslate">
chmod 777 app/cache app/logs
</pre>
<p>Point your server to &#8220;<em>web/</em>&#8221; as root directory and you already would have to see symfony2 welcome page at <a target="_blank"  href="http://127.0.0.1/app_dev.php/">http://127.0.0.1/app_dev.php/</a></p>
<p><span id="more-7"></span></p>
<p>&nbsp;</p>
<h2>2. Setup database</h2>
<p>Setup <em>app/config/parameters.ini</em> with your database options (you also can do it from <a href="http://127.0.0.1/app_dev.php/_configurator/">http://127.0.0.1/app_dev.php/_configurator/</a> ), for mysql would be something like:</p>
<pre class="brush: plain; title: ; notranslate">
[parameters]
    database_driver=pdo_mysql
    database_host=localhost
    database_name=symfony2
    database_user=symfony2
    database_password=password
    mailer_transport=smtp
    mailer_host=localhost
    mailer_user=
    mailer_password=
    locale=en
    csrf_secret=op234j234j2424jojpfwesdcsdc
</pre>
<p>&nbsp;</p>
<h2>3. Creating our first bundle</h2>
<p>Now we already can create a bundle:</p>
<pre class="brush: bash; title: ; notranslate">
php app/console init:bundle &quot;Montes\AdictosBundle&quot; src
</pre>
<p>Now add this to <em>app/autoload.php</em> :</p>
<pre class="brush: php; title: ; notranslate">
$loader-&gt;registerNamespaces(array(
    'Montes'                         =&gt; __DIR__.'/../src',
    // ...
));
</pre>
<p>and this to <em>app/AppKernel.php</em> :</p>
<pre class="brush: php; title: ; notranslate">
    $bundles = array(
        // ...
        new Montes\AdictosBundle\MontesAdictosBundle(),
    );
</pre>
<p>&nbsp;</p>
<h2>4. Route</h2>
<p>In order to symfony2 knows where it must send requests, we add to <em>app/config/routing.yml</em> :</p>
<pre class="brush: plain; title: ; notranslate">
homepage:
    pattern:  /adictos
    defaults: { _controller: MontesAdictosBundle:Default:index }
</pre>
<p>At this point we already have our new bundle working at: <a target="_blank" href="http://127.0.0.1/app_dev.php/adictos">http://127.0.0.1/app_dev.php/adictos</a></p>
<p>&nbsp;</p>
<h2>5. Controller</h2>
<p>The default controller (<em>Montes/AdictosBundle/Controller/DefaultController.php</em>) has been already created by Symfony2, now we are going to create the <em>StoreController.php</em></p>
<pre class="brush: php; title: ; notranslate">
//Montes/AdictosBundle/Controller/StoreController.php
&lt;?php

namespace Montes\AdictosBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class StoreController extends Controller
{
    /**
     * @Template()
     */
    public function indexAction($store)
    {
        return array('store' =&gt; $store);
    }
}
</pre>
<p>an add to <em>app/config/routing_dev.yml</em></p>
<pre class="brush: plain; title: ; notranslate">
store:
    pattern: /adictos/{store}
    defaults: { _controller: MontesAdictosBundle:Store:index }
</pre>
<p>and a new twig&#8217;s template at <em>Montes/AdictosBundle/Resources/Views/Store/index.html.twig</em></p>
<pre class="brush: xml; title: ; notranslate">
So you want store &quot;{{ store }}&quot;?
</pre>
<p>And now going to <a target="_blank" href="http://127.0.0.1/app_dev.php/adictos/my-store">http://127.0.0.1/app_dev.php/adictos/my-store</a> we&#8217;ll get:</p>
<p><em>So you want store &#8220;my-store&#8221;?</em></p>
<p>&nbsp;</p>
<h2>6. Model</h2>
<p><del datetime="2011-05-24T13:07:16+00:00">First we add to <em>app/config/config.yml</em> &#8220;<em>MontesAdictosBundle: ~</em>&#8220;</del> From Beta1 mapping is auto by default.</p>
<p>We start with Doctrine2, we are going to define our model for store/category. Before to start, we must to create the folder <em>Montes/AdictosBundle/Entity</em> where we&#8217;ll save all of them.</p>
<p>And we can start, model for Store:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
// Montes/AdictosBundle/Entity/Store.php

namespace Montes\AdictosBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Store
{
    /**
     * @ORM\Id
     * @ORM\Column(type=&quot;integer&quot;)
     * @ORM\GeneratedValue(strategy=&quot;AUTO&quot;)
     */
     protected $id;

    /**
     * @ORM\ManyToMany(targetEntity=&quot;Category&quot;)
     * @ORM\JoinTable(name=&quot;stores_categories&quot;,
     *      joinColumns={@ORM\JoinColumn(name=&quot;store_id&quot;, referencedColumnName=&quot;id&quot;)},
     *      inverseJoinColumns={@ORM\JoinColumn(name=&quot;category_id&quot;, referencedColumnName=&quot;id&quot;)})
     */
    protected $categories;

    /**
     * @ORM\Column(type=&quot;string&quot;, length=&quot;255&quot;)
     */
    protected $url;

    /**
     * @ORM\Column(type=&quot;string&quot;, length=&quot;255&quot;)
     */
    protected $name;

    /**
     * @ORM\Column(type=&quot;integer&quot;)
     */
    protected $clicks = 0;

    /**
     * @ORM\Column(type=&quot;boolean&quot;)
     */
    protected $validated = false;

    /**
     * @ORM\Column(type=&quot;integer&quot;)
     */
    protected $pcomments = 0;

    /**
     * @ORM\Column(type=&quot;integer&quot;)
     */
    protected $ncomments = 0;

    /**
     * @ORM\Column(type=&quot;boolean&quot;)
     */
    protected $active = false;

    /**
     * @ORM\Column(type=&quot;datetime&quot;, name=&quot;updated_at&quot;)
     */
    protected $updatedAt;

    /**
     * @ORM\Column(type=&quot;datetime&quot;, name=&quot;created_at&quot;)
     */
    protected $createdAt;

    public function __construct()
    {
        $this-&gt;categories = new \Doctrine\Common\Collections\ArrayCollection();
        $this-&gt;createdAt = new \DateTime();
        $this-&gt;updatedAt = new \DateTime();
    }
}
</pre>
<p>Model for Category:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
// Montes/AdictosBundle/Entity/Category.php

namespace Montes\AdictosBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type=&quot;integer&quot;)
     * @ORM\GeneratedValue(strategy=&quot;AUTO&quot;)
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity=&quot;Category&quot;, mappedBy=&quot;parent&quot;)
     */
    protected $children;

    /**
     * @ORM\ManyToOne(targetEntity=&quot;Category&quot;, inversedBy=&quot;children&quot;)
     * @ORM\JoinColumn(name=&quot;parent_id&quot;, referencedColumnName=&quot;id&quot;)
     * @ORM\Column(nullable=&quot;true&quot;)
     */
    protected $parent;

    /**
     * @ORM\ManyToMany(targetEntity=&quot;Store&quot;, mappedBy=&quot;categories&quot;)
     */
    protected $stores;

    /**
     * @ORM\Column(type=&quot;string&quot;, length=&quot;255&quot;)
     */
    protected $name;

    /**
     * @ORM\Column(type=&quot;string&quot;, length=&quot;255&quot;, name=&quot;url_string&quot;, unique=&quot;true&quot;)
     */
    protected $urlString;

    public function __construct()
    {
        $this-&gt;stores = new \Doctrine\Commmon\Collections\ArrayCollection();
    }
}
</pre>
<p>And now we go to terminal and run this to generate the database tables:</p>
<pre class="brush: bash; title: ; notranslate">
php app/console doctrine:schema:create
</pre>
<p>and to complete our model with its getters/setters:</p>
<pre class="brush: bash; title: ; notranslate">
php app/console doctrine:generate:entities MontesAdictosBundle
</pre>
<p>&nbsp;</p>
<h2>7. Testing the model</h2>
<p>We&#8217;re going to change the action that shows our store to it&#8217;s own action (storeAction) and now the indexAction will show how many stores we have in the database.</p>
<pre class="brush: plain; title: ; notranslate">
# app/config/routing_dev.yml
store:
    pattern: /adictos/{store}
    defaults: { _controller: MontesAdictosBundle:Store:store }

store_index:
    pattern: /adictos/
    defaults: { _controller: MontesAdictosBundle:Store:index }
</pre>
<pre class="brush: php; title: ; notranslate">
&lt;?php
// Montes/AdictosBundle/Controller/StoreController.php

namespace Montes\AdictosBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class StoreController extends Controller
{

    /**
     * @Template()
     */
    public function indexAction()
    {
        $em = $this-&gt;get('doctrine.orm.entity_manager');
        $stores = $em-&gt;createQuery('SELECT count(s.id) AS total FROM Montes\AdictosBundle\Entity\Store s')-&gt;getSingleScalarResult();
        return array('stores' =&gt; $stores);
    }

    /**
     * @Template()
     */
    public function storeAction($store)
    {
        return array('store' =&gt; $store);
    }
}
</pre>
<pre class="brush: xml; title: ; notranslate">
&lt;!-- Montes/AdictosBundle/Resources/views/Store/index.html.twig --&gt;
We have a total of {{ stores }} stores.
</pre>
<pre class="brush: xml; title: ; notranslate">
&lt;!-- Montes/AdictosBundle/Resources/views/Store/store.html.twig --&gt;
So you want store &quot;{{ store }}&quot;?
</pre>
<p>Now when browsing to <a target="_blank" href="http://127.0.0.1/app_dev.php/adictos/">http://127.0.0.1/app_dev.php/adictos/</a> we&#8217;ll get <em>We have a total of 0 stores</em> and at <a target="_blank" href="http://127.0.0.1/app_dev.php/adictos/my-store">http://127.0.0.1/app_dev.php/adictos/my-store</a> we&#8217;ll get <em>So you want store &#8220;my-store&#8221;?</em></p>
]]></content:encoded>
			<wfw:commentRss>http://en.mooontes.com/2011/03/27/symfony2-ultra-fast-start-tutorial/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

