Sunday, April 10, 2011

Symfony 2 create schema and entities from database already exist

hello it's my new tutorial for symfony 2
how i can create schema or entities from database already exist?
ok for create db with graphic assistant you can use for example mysql-workbench yes very cool tool for speed up your production ;
i know lot of developers choose inverse concept : start by creating classes of entities or schema ; then create db from one  of the two last .
ok link of tut : http://www.youtube.com/watch?v=UASGfBCzziI
and  link for  download : http://wb.mysql.com/?category_name=windows

ok now you have sql ;  import .sql with phpmyadmin or other technique

ok goto :   project symfony name->app->config->parameters.ini
and change :
[parameters]
    database_driver=pdo_mysql
    database_host=localhost  
    database_name=symfony    change with you data base name
    database_user=root      you can change with you personel name
    database_password=   ligne for your password
    mailer_transport=smtp
    mailer_host=localhost
    mailer_user=
    mailer_password=
    locale=en
    csrf_secret=xxxxxxxxxx

 we assume you already have a bundle with name yourbundle
ok goto :  project symfony folder->app->config->config.yml
and replace or configure  the name of the bundle (see last ligne) :
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        logging:  %kernel.debug%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        mappings:
            AcmeDemoBundle: ~    replace with   yourbundle:
~ 


note: yourbundle is the name of class .php for example you have a bundle by default in package symfony ;
ok goto project symfony folder->src->Acme->DemoBundle->AcmeDemoBundle.php
AcmeDemoBundle is the name of the bundle

you can create you own bundle with commande 
point with dos in project symfony  example cd \xampp\htdocs\Symfony2 
and tap php app/console init:bundle Bundles\ yourbundle src
result : you have a new bundle in folder src
then declare you bundle in appkernel.php then declare path of namespace in autoload.php all in folder app

note:if you use windows xp ; you must declare path of php.exe in environment variable


then for create all maps of evrey table in db 
tap command 
C:\xampp\htdocs\projectprv>php app/console doctrine:mapping:import "yourbundle" yml --em=default
that because symfony use map format for create entities ;you must   symfony1.4 already have schema.yml
and goto : project symfony folder->src->Acme->DemoBundle->Resources->config->doctrine->metadata->orm
now we have your schema separete on many file with format yml 

ok for creating entities (in symfony1.4 models classes)  
tap:
C:\xampp\htdocs\projectprv>php app/console doctrine:generate:entities yourbundle
and goto :  
project symfony folder->src->Bundles->testBundle->Entity
now you see  all entities


No comments:

Post a Comment