码迷,mamicode.com
首页 > 其他好文 > 详细

Symfony2 Doctrine ORM 安装配置

时间:2018-12-13 19:31:43      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:generated   通过   lob   strategy   style   cut   with   blog   test   

第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 Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        );

        // ...
    }

    // ...
}

 

配置数据库

数据库配置文件

# 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 AppBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
 
/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
 
    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;
 
    /**
     * @ORM\Column(type="decimal", scale=2)
     */
    private $price;
 
    /**
     * @ORM\Column(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 AppBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
 
// ...
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

 

Symfony2 Doctrine ORM 安装配置

标签:generated   通过   lob   strategy   style   cut   with   blog   test   

原文地址:https://www.cnblogs.com/jiafeimao-dabai/p/10115186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!