Doctrine - Puzzlout/DevGuidelines GitHub Wiki
Doctrine comes with separate commands for generating entities, database and other stuff.
Open up app/config/parameters.yml, we can setup params for db access here, I think it's all clear.
Open up app/config/config.yml, find doctrine: key. You can change charset there if you wish and driver is set to pdo_mysql for mysql connection, other stuff is included through parameters.yml file (i.e. host: %database_host%).
If you setup everything like you should you can run php bin/console doctrine:database:create
it will create database you specified in parameters.yml with user you specified there in charset that you setup in config.yml.
After that you are ready to generate entities for doctrine, run php bin/console doctrine:generate:entity
, type entity name in format of NameBundle/EntityName (i.e. AppBundle:Product) choose annotations, you will then be prompted to enter name of the fields you want to add with their types and if they are unique or nullable. After you add all entities you want you can check them out in Entity directory of your Bundle. Everything should be generated together with getters and setters.
To persist them in DB, run php bin/console doctrine:schema:update --force
(force param will persist it in db, without it you'll get sql dump to double check before persisting if im not mistaken).
You can also create them manually by creating Name.php file in Entity directory, take care of namespace and include Doctrine\ORM\Mapping as ORM; if you want to use annotations (check those automatically generated).
For relations see http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html
You can add manually new fields to existing entities, if you do you can always run php bin/console doctrine:generate:entities AppBundle/Product
to update AppBundle/Product entity or php bin/console doctrine:generate entities AppBundle
to update all entities inside AppBundle. When you update them don't worry if there are no changes on some of them they will stay the same, only new getters/setters will be created so no worries. Don't forget to run php bin/console doctrine:schema:update --force
after each entity update so you persist those changes in files in the DB.
References
- http://symfony.com/doc/current/book/doctrine.html - Symfony and Doctrine
- http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html - Doctrine relation documentation
- http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/annotations-reference.html - Doctrine annotation reference