• Symfony2 Doctrine ORM 安装配置


    第1步:下载Bundle

    $ composer require doctrine/doctrine-bundle

    此命令要求您全局安装Composer,如Composer文档安装章节中所述

    DoctrineBundle git安装包  https://github.com/doctrine

    第2步:启用Bundle

    然后,通过在app/AppKernel.php 项目文件中添加以下行来启用该包

    <?php
    // app/AppKernel.php
    
    // ...
    class AppKernel extends Kernel
    {
        public function registerBundles()
        {
            $bundles = array(
                // ...
    
                new DoctrineBundleDoctrineBundleDoctrineBundle(),
            );
    
            // ...
        }
    
        // ...
    }

    配置数据库

    数据库配置文件

    # app/config/parameters.yml
    parameters:
        database_host:      localhost
        database_name:      test_project
        database_user:      root
        database_password:  password
     
    # ...

    应用配置文件

    doctrine:
        dbal:
            driver:   %database_driver%
            host:     %database_host%
            port:     %database_port%
            dbname:   %database_name%
            user:     %database_user%
            password: %database_password%
            charset:  UTF8
            # if using pdo_sqlite as your database driver, add the path in parameters.yml
            # e.g. database_path: %kernel.root_dir%/data/data.db3
            # path:     %database_path%
            mapping_types:
                enum: string
                set: string
                varbinary: string
                tinyblob: text
        orm:
            auto_generate_proxy_classes: %kernel.debug%
            auto_mapping: true

    创建实体类

    // src/AppBundle/Entity/Product.php
    namespace AppBundleEntity;
     
    use DoctrineORMMapping as ORM;
     
    /**
     * @ORMEntity
     * @ORMTable(name="product")
     */
    class Product
    {
        /**
         * @ORMColumn(type="integer")
         * @ORMId
         * @ORMGeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @ORMColumn(type="string", length=100)
         */
        private $name;
     
        /**
         * @ORMColumn(type="decimal", scale=2)
         */
        private $price;
     
        /**
         * @ORMColumn(type="text")
         */
        private $description;
    }

    doctrine 命令
    php app/console doctrine:generate:entity 创建实体类

    php app/console doctrine:schema:update --dump-sql 展示sql的差异

    php app/console doctrine:schema:update --force --full-database 创建数据表

    php app/console doctrine:schema:validate 实体类添加映射信息

    php app/console doctrine:generate:entities AppBundle/Entity/Product 实体类生成Getters和Setters

    持久化对象到数据库

    // src/AppBundle/Controller/DefaultController.php
     
    // ...
    use AppBundleEntityProduct;
    use SymfonyComponentHttpFoundationResponse;
     
    // ...
    public function createAction()
    {
        $product = new Product();
        $product->setName('Keyboard');
        $product->setPrice(19.99);
        $product->setDescription('Ergonomic and stylish!');
     
        $em = $this->getDoctrine()->getManager();
     
        // tells Doctrine you want to (eventually) save the Product (no queries yet)
        // 告诉Doctrine你希望(最终)存储Product对象(还没有语句执行)
        $em->persist($product);
     
        // actually executes the queries (i.e. the INSERT query)
        // 真正执行语句(如,INSERT 查询)
        $em->flush();
     
        return new Response('Saved new product with id '.$product->getId());
    }

    参考地址:http://www.symfonychina.com/doc/current/doctrine.html

         https://stackoverflow.com/questions/13670471/database-fail-the-database-schema-is-not-in-sync-with-the-current-mapping-file

         https://my.oschina.net/imot/blog/176661

  • 相关阅读:
    luogu 2617
    BZOJ 3295
    BZOJ 2458
    luogu 3810
    Uva
    Uva
    Uva
    Uva
    Uva
    成员函数的const到底修饰的是谁
  • 原文地址:https://www.cnblogs.com/jiafeimao-dabai/p/10115186.html
Copyright © 2020-2023  润新知