Symfony2 Doctrine ORM 安装配置
Posted 加菲猫and大白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Symfony2 Doctrine ORM 安装配置的主要内容,如果未能解决你的问题,请参考以下文章
使用 YAML 的 Doctrine2 和 Symfony2 的默认列值?
级联持久性不工作(Doctrine ORM + Symfony 2)
在 Doctrine (symfony2) 中对可翻译实体进行版本控制
Symfony2 和 Doctrine - 错误:无效的 PathExpression。必须是 StateFieldPathExpression