Building application from MySQL Workbench to MVP in Symfony2 using Doctrine
Would you like to generate basic CRUD application in PHP just from the database? I can show you how to do it in 5 minutes!
A long time ago I was interested in PHP Symfony framework. Using some well prepared commands you can generate minimum viable product just from database 🙂 Please follow those steps.
Short warning: notes are from 2013/14 and may have some difference to newest versions. If you will find anything, please share it in comment – lots of people will be grateful.
Let’s generate our app with those steps!
0. Prepare your database model in MySQL Workbench.
1. How to generate new bundle in Symfony2?
php app/console generate:bundle –namespace=Pdb/DutyBundle –format=yml
2. How to check/debug routing in Symfony2?
php app/console router:debug
3. How to import database model and create php entities with it?
php app/console doctrine:mapping:convert yml ./src/Pdb/DutyBundle/Resources/config/doctrine/metadata/orm –from-database –force
You should have database credentials added to the Symfony2 config files.
4. How to generate entities from choosen bundle in Symfony2?
php app/console doctrine:generate:entities PdbDutyBundle
5. How to add annotations to generated entities in Symfony2?
php app/console doctrine:mapping:import PdbDutyBundle annotation
6. How to generate getters/setters in entities from choosen bundle?
php app/console doctrine:generate:entities PdbDutyBundle
7. How to generate CRUD operations for choosen entity?
php app/console doctrine:generate:crud –entity=PdbDutyBundle:Duty –route-prefix=duty –with-write –format=yml
Some useful commands:
8. How to clean app cache?
php app/console cache:clear –env=prodphp app/console cache:clear –env=dev
9. Send schema to database:
php app/console doctrine:schema:update –force
10. How to load fixtures to database – remember: it does PURGE automatically!
php app/console doctrine:fixtures:load
11. How to handle dates in Symfony2?
public function __toString(){ return $this->getDisplayedname() ? $this->getDisplayedname() : „”;}
$date = new \DateTime();$this->registrationdata = date_create($date->format(’Y-m-d H:i:s’));