Thứ Năm, 18 tháng 6, 2015

Symfony2: Create, edit model entity and update schema

After install Symfony 2 Installer, create new project named blog with command:
symfony new blog

Edit file app/config/parameters.yml, fill database connection info

Create entity Post with two fields(name and description) by command:
php app/console doctrine:generate:entity --entity=AppBundle:Post --format=annotation --fields="name:string(255) description:string(255)" --no-interaction

Update DB, create Post table
php app/console cache:clear
php app/console doctrine:schema:update —force

Update entity Post

Add new property named address:
    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=255)
     */
    private $address;

Generate getter and setter for address by run command:
php app/console doctrine:generate:entities AppBundle:Post

Update app/config/configyml like this
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
                mapping_types:
                    enum: string
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        #naming_strategy: doctrine.orm.naming_strategy.underscore
        #auto_mapping: true
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    AppBundle: ~


Then, run update schema with entity_manager is default
php app/console doctrine:schema:update --em=default










- Comments

0 nhận xét:

Đăng nhận xét