Symfony2 quick tips: How to generate bundle and how to create simple entity + CRUD?

Step 1.

What is the Symfony bundle?

“A bundle is nothing more than a directory that houses everything related to a specific feature, including PHP classes, configuration, and even stylesheets and Javascript files…”

It’s just a quote from a Symfony2 documentation: http://symfony.com/doc/current/book/page_creation.html

I generated new bundle with this command:

 php app/console generate:bundle  

and I used the Symfony2 (I used the current Symfony version: Symfony 2.2.0) command line wizard because it is very comfortable It made the bundle “skeleton”, create routings and something like this. I gave the “TestCompany/TestBundle” namespace as well as I used annotations. After the bundle generation you should find your generated bundle under the /src/TestCompany/TestBundle path.

Step 2.

Create new database entity. I used Doctrine and MySQL database and I wrote my entity classes manually.

Don’t forget set the database configuration in the app/config/parameters.yml.

 parameters:  
   database_driver: pdo_mysql  
   database_host: 127.0.0.1  
   database_port: null  
   database_name: symfony2  
   database_user: yourusername  
   database_password: yourpass  
   mailer_transport: smtp  
   mailer_host: 127.0.0.1  
   mailer_user: null  
   mailer_password: null  
   locale: en  
   secret: a87c3c69d4b934488ab10f9272c1ba9b3  
   database_path: null  

So, I created two simple entity: Test, Category. Both entity has two fileds: id, title.  Between Test and Category is Many-to-many relationship.

/src/TestCompany/TestBundle/Entity/Test.php:

 <?php  

 namespace TestCompany\TestBundle\Entity;  

 use Doctrine\ORM\Mapping as ORM;  
 use Symfony\Component\Validator\Constraints as Assert;  

 /**  
  * @ORM\Entity  
  * @ORM\Table(name="test")  
  */  
 class Test {  

   /**  
    * @ORM\Id  
    * @ORM\Column(type="integer")  
    * @ORM\GeneratedValue(strategy="AUTO")  
    */  
   protected $id;  

   /**  
    * @ORM\Column(type="string", length=255, nullable=true)  
    * @Symfony\Component\Validator\Constraints\NotBlank  
    * @Assert\MaxLength(255)  
    * @var string  
    */  
   private $title;  

   /**  
    * @ORM\ManyToMany(targetEntity="Category")  
    * @ORM\JoinTable(name="test_category",  
    *   joinColumns={@ORM\JoinColumn(name="test_id", referencedColumnName="id", nullable=true)},  
    *   inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", columnDefinition="INT NULL")}  
    *   )  
    */  
   private $category;  

 }  

The title field is validated – it can’t be empty.

 @Symfony\Component\Validator\Constraints\NotBlank   

It is just simple example definitely the real world is more complicated. It may be that you need to write on your own validators etc. You should read this documentation: http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

/src/TestCompany/TestBundle/Entity/Category.php:

 <?php  

 namespace TestCompany\TestBundle\Entity;  

 use Doctrine\ORM\Mapping as ORM;  
 use Doctrine\Common\Collections\ArrayCollection;  

 /**  
  * @ORM\Entity  
  * @ORM\Table(name="category")  
  */  
 class Category {  

   /**  
    * @ORM\Id  
    * @ORM\Column(type="integer")  
    * @ORM\GeneratedValue(strategy="AUTO")  
    */  
   protected $id;  

   /**  
    * @ORM\Column(type="string")  
    * @Symfony\Component\Validator\Constraints\NotBlank  
    * @var string  
    */  
   private $title;  

   public function __toString() {  
     return $this->title;  
   }  

 }  

The __toString() method is very important because It return the category title as string without it for example won’t be able to work generated CRUD controllers as well as on your own controllers etc.

So, after that you should generate entities:

 php app/console doctrine:generate:entities TestCompanyTestBundle  

…then you can try the Symfony2 CRUD generator:

 php app/console generate:doctrine:crud  

or you can use this command without interactions:

 php app/console generate:doctrine:crud --entity=TestCompanyTestBundle:Test --format=annotation --with-write --no-interaction  

but before the CRUD generation you have to create database, create tables and indexes:

 php app/console doctrine:database:create  

 php app/console doctrine:schema:update --force  

The doctrine:schema:update command has two parameters:

doctrine:schema:update –force to execute the command
doctrine:schema:update –dump-sql to dump the SQL statements to the screen

I think you will need some fixtures in the category table. So, You should try the DoctrineFixturesBundle:

http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html

If all is well :-), you should find your CRUD on this URL:

http://localhost/YOURSYMFONYDIR/web/app_dev.php/test

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to Symfony2 quick tips: How to generate bundle and how to create simple entity + CRUD?

  1. Beniston says:

    Cool. I was searching for such a help to start my Symfony. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s