Doctrine

这一章将介绍Symfony2的数据库操作。在Symfony2中默认使用的ORM库是Doctrine。

关于Doctrine

Doctrine是一个专门用于处理数据库操作的ORM库,关于ORM的介绍请参考: http://zh.wikipedia.org/wiki/对象关系映射

它基于一个强大的dbal(数据库抽象层) ( http://www.doctrine-project.org/projects/dbal.html ) ,让我们可以透过PHP PDO达到数据存储的抽象化。同时它还提供了一个类似SQL语法的操作,称之为DQL(Doctrine查询语言)。这让我们可以将数据库的数据作为PHP的对象进行操作,而不用关心数据库的细节。这也使得我们可以方便的切换数据库后端引擎。

数据库配置

前面有说到过程序总共有devtestprod这三种运行环境。为了方便开发测试,我们在开发环境和测试环境中使用SQLite数据库,在生产环境中使用MySQL数据库。

先配置生产环境的数据库。编辑文件app/config/parameters.yml找到database_开头的参数。根据自己的MySQL信息进行修改。

再配置开发环境的数据库。编辑文件app/config/config_dev.yml,添加如下内容:

  1. doctrine:
  2. dbal:
  3. driver: pdo_sqlite
  4. path: "%kernel.root_dir%/data.db3"
  5. charset: UTF8
  6. orm:
  7. auto_generate_proxy_classes: "%kernel.debug%"
  8. auto_mapping: true

创建数据库模型

数据库模型(Model)称之为实体(Entity)。执行如下命令创建一个User实体:

  1. $ php app/console generate:doctrine:entity --entity=BloggerBlogBundle:User --format=annotation --with-repository --no-interaction --fields="name:string(100) email:string(100)"

通过这个命令创建的User实体有两个字段:nameemail。这将会生成两个文件:src/Blogger/BlogBundle/Entity/User.phpsrc/Blogger/BlogBundle/Entity/UserRepository.php。这个类似于Rails的:

  1. $ rails generate scaffold User name:string email:string

User.php文件内容大致如下:

  1. /**
  2. * User
  3. *
  4. * @ORM\Table(name="user")
  5. * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Entity\UserRepository")
  6. */
  7. class User
  8. {
  9. /**
  10. * @var integer
  11. *
  12. * @ORM\Column(name="id", type="integer")
  13. * @ORM\Id
  14. * @ORM\GeneratedValue(strategy="AUTO")
  15. */
  16. private $id;
  17. /**
  18. * @var string
  19. *
  20. * @ORM\Column(name="name", type="string", length=100)
  21. */
  22. private $name;
  23. /**
  24. * @var string
  25. *
  26. * @ORM\Column(name="email", type="string", length=100)
  27. */
  28. private $email;

前面说过了在注释中以"@"符号开头的语言是有特殊意义的。它是annotation格式的配置信息,在这里是用于定义数据库的结构。关于这些配置说明可参考Doctrine的文档: http://www.doctrine-project.org/docs/orm/2.0/en/reference/basic-mapping.html#doctrine-mapping-types

接着再创建Blog实体和Comment实体:

  1. php app/console generate:doctrine:entity --entity=BloggerBlogBundle:Blog --format=annotation --with-repository --no-interaction --fields="title:string(255) author:integer blog:text image:string(50) tags:text created:datetime updated:datetime"
  2. php app/console generate:doctrine:entity --entity=BloggerBlogBundle:Comment --format=annotation --with-repository --no-interaction --fields="user:string comment:text approved:boolean blog:integer created:datetime updated:datetime"

其中Blog用于保存文章,Comment用于保存文章的评论。这两个实体应该是需要有关联的,并且Blog还应该与User有关联。

编辑Blog.php文件,修改如下:

  1. /**
  2. * @ORM\OneToMany(targetEntity="Comment", mappedBy="blog")
  3. **/
  4. private $comments;
  5. /**
  6. * @var User
  7. *
  8. * @ORM\ManyToOne(targetEntity="User")
  9. * @ORM\JoinColumn(name="author_id", referencedColumnName="id", onDelete="CASCADE")
  10. **/
  11. private $author;
  12. /**
  13. * Set author
  14. *
  15. * @param \Blogger\BlogBundle\Entity\User $author
  16. *
  17. * @return Blog
  18. */
  19. public function setAuthor(\Blogger\BlogBundle\Entity\User $author)
  20. {
  21. $this->author = $author;
  22. return $this;
  23. }
  24. /**
  25. * Get author
  26. *
  27. * @return \Blogger\BlogBundle\Entity\User
  28. */
  29. public function getAuthor()
  30. {
  31. return $this->author;
  32. }

编辑Comment.php文件,修改如下:

  1. /**
  2. * @var Blog
  3. *
  4. * @ORM\ManyToOne(targetEntity="Blog", inversedBy="comments")
  5. * @ORM\JoinColumn(name="blog_id", referencedColumnName="id", onDelete="CASCADE")
  6. */
  7. private $blog;
  8. /**
  9. * Set blog
  10. *
  11. * @param \Blogger\BlogBundle\Entity\Blog $blog
  12. *
  13. * @return Comment
  14. */
  15. public function setBlog(\Blogger\BlogBundle\Entity\Blog $blog)
  16. {
  17. $this->blog = $blog;
  18. return $this;
  19. }
  20. /**
  21. * Get blog
  22. *
  23. * @return \Blogger\BlogBundle\Entity\Blog
  24. */
  25. public function getBlog()
  26. {
  27. return $this->blog;
  28. }

上面的ManyToOneOneToMany指明了多对一和一对多的映射关系,在数据库中是通过外键关联来完成的。

然后再执行如下命令更新实体结构:

  1. php app/console doctrine:generate:entities Blog

命令执行完成之后Blog实体增加了三个方法进行操作$comments属性。

接下来执行命令创建数据库:

  1. $ php app/console doctrine:database:create

再执行命令创建数据表:

  1. $ php app/console doctrine:schema:create

新建控制器

Blog实体已创建,接下来将创建一个页面用于显示文章内容。

首先添加路由配置:

  1. # src/Blogger/BlogBundle/Resources/config/routing.yml
  2. blogger_blogBundle_blog_show:
  3. pattern: /{id}
  4. defaults: { _controller: BloggerBlogBundle:Blog:show }
  5. requirements:
  6. _method: GET
  7. id: \d+

然后再新建一个控制器BlogController

  1. $ php app/console generate:controller --controller=BloggerBlogBundle:Blog --route-format=yml

接着为BlogController创建一个showAction方法:

  1. /**
  2. * Show a blog entry
  3. *
  4. * @param integer $id
  5. *
  6. * @return array
  7. *
  8. * @Template()
  9. */
  10. public function showAction($id)
  11. {
  12. $em = $this->getDoctrine()->getEntityManager();
  13. $blog = $em->getRepository('BloggerBlogBundle:Blog')->find($id);
  14. if (!$blog) {
  15. throw $this->createNotFoundException('Unable to find Blog post.');
  16. }
  17. return array('blog' => $blog);
  18. }

最后再为该页面创建模板一个文件:

  1. {# src/Blogger/BlogBundle/Resouces/views/Blog/show.html.twig #}
  2. {% extends 'BloggerBlogBundle::layout.html.twig' %}
  3. {% block title %}{{ blog.title }}{% endblock %}
  4. {% block body %}
  5. <article class="blog">
  6. <header>
  7. <div class="date"><time datetime="{{ blog.created|date('c') }}">{{ blog.created|date('l, F j, Y') }}</time></div>
  8. <h2>{{ blog.title }}</h2>
  9. </header>
  10. <img src="{{ asset(['images/', blog.image]|join) }}" alt="{{ blog.title }} image not found" class="large" />
  11. <div>
  12. <p>{{ blog.blog }}</p>
  13. </div>
  14. </article>
  15. {% endblock %}

为了美观再添加一些样式,src/Blogger/BlogBundle/Resouces/public/css/blog.css

  1. .date { margin-bottom: 20px; border-bottom: 1px solid #ccc; font-size: 24px; color: #666; line-height: 30px }
  2. .blog { margin-bottom: 20px; }
  3. .blog img { width: 190px; float: left; padding: 5px; border: 1px solid #ccc; margin: 0 10px 10px 0; }
  4. .blog .meta { clear: left; margin-bottom: 20px; }
  5. .blog .snippet p.continue { margin-bottom: 0; text-align: right; }
  6. .blog .meta { font-style: italic; font-size: 12px; color: #666; }
  7. .blog .meta p { margin-bottom: 5px; line-height: 1.2em; }
  8. .blog img.large { width: 300px; min-height: 165px; }

再起用浏览器打开页面 http://127.0.0.1:8000/1 看看效果,应该会如图显示出现404错误:

第5章 Doctrine  - 图1

这是由于数据库空的没有数据,因而无法显示。

添加测试数据

为了方便测试我们向数据库中添加一些测试数据。

首先编辑composer.json文件,在require字段中新增两项依赖:

  1. "doctrine/doctrine-fixtures-bundle": "dev-master",
  2. "doctrine/data-fixtures": "dev-master",

然后执行命令进行安装:

  1. $ composer.phar update

并且在app/AppKernel.php进行注册,由于这个只在开发阶段使用,因此将其注册在开发环境中:

  1. if (in_array($this->getEnvironment(), array('dev', 'test'))) {
  2. // ...
  3. $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
  4. // ...
  5. }

然后编写测试数据,新建文件src/Blogger/BlogBundle/DataFixtures/ORM/UserFixtures.php

  1. <?php
  2. namespace Blogger\BlogBundle\DataFixtures\ORM;
  3. use Doctrine\Common\DataFixtures\AbstractFixture;
  4. use Doctrine\Common\DataFixtures\FixtureInterface;
  5. use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
  6. use Doctrine\Common\Persistence\ObjectManager;
  7. use Blogger\BlogBundle\Entity\User;
  8. /**
  9. * UserFixtures
  10. */
  11. class UserFixtures extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
  12. {
  13. /**
  14. * {@inheritDoc}
  15. */
  16. public function load(ObjectManager $manager)
  17. {
  18. $user = new User();
  19. $user->setName("someuser");
  20. $user->setEmail("someuser@mail.ca");
  21. $manager->persist($user);
  22. $manager->flush();
  23. $this->addReference('user-1', $user);
  24. }
  25. /**
  26. * @return int
  27. */
  28. public function getOrder()
  29. {
  30. return 1;
  31. }
  32. }

再新建文件src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php

  1. <?php
  2. namespace Blogger\BlogBundle\DataFixtures\ORM;
  3. use Doctrine\Common\DataFixtures\AbstractFixture;
  4. use Doctrine\Common\DataFixtures\FixtureInterface;
  5. use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
  6. use Doctrine\Common\Persistence\ObjectManager;
  7. use Blogger\BlogBundle\Entity\Blog;
  8. /**
  9. * BlogFixtures
  10. */
  11. class BlogFixtures extends AbstractFixture implements OrderedFixtureInterface
  12. {
  13. /**
  14. * {@inheritDoc}
  15. */
  16. public function load(ObjectManager $manager)
  17. {
  18. $user1 = $this->getReference('user-1');
  19. $blog1 = new Blog();
  20. $blog1->setTitle('A day with Symfony2');
  21. $blog1->setBlog('Lorem ipsum dolor sit amet, consectetur adipiscing eletra electrify denim vel ports.\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ut velocity magna. Etiam vehicula nunc non leo hendrerit commodo. Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque. Nulla consectetur tempus nisl vitae viverra. Cras el mauris eget erat congue dapibus imperdiet justo scelerisque. Nulla consectetur tempus nisl vitae viverra. Cras elementum molestie vestibulum. Morbi id quam nisl. Praesent hendrerit, orci sed elementum lobortis, justo mauris lacinia libero, non facilisis purus ipsum non mi. Aliquam sollicitudin, augue id vestibulum iaculis, sem lectus convallis nunc, vel scelerisque lorem tortor ac nunc. Donec pharetra eleifend enim vel porta.');
  22. $blog1->setImage('beach.jpg');
  23. $blog1->setAuthor($user1);
  24. $blog1->setTags('symfony2, php, paradise, symblog');
  25. $blog1->setCreated(new \DateTime());
  26. $blog1->setUpdated($blog1->getCreated());
  27. $manager->persist($blog1);
  28. $blog2 = new Blog();
  29. $blog2->setTitle('The pool on the roof must have a leak');
  30. $blog2->setBlog('Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque. Na. Cras elementum molestie vestibulum. Morbi id quam nisl. Praesent hendrerit, orci sed elementum lobortis.');
  31. $blog2->setImage('pool_leak.jpg');
  32. $blog2->setAuthor($user1);
  33. $blog2->setTags('pool, leaky, hacked, movie, hacking, symblog');
  34. $blog2->setCreated(new \DateTime("2011-07-23 06:12:33"));
  35. $blog2->setUpdated($blog2->getCreated());
  36. $manager->persist($blog2);
  37. $blog3 = new Blog();
  38. $blog3->setTitle('Misdirection. What the eyes see and the ears hear, the mind believes');
  39. $blog3->setBlog('Lorem ipsumvehicula nunc non leo hendrerit commodo. Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque.');
  40. $blog3->setImage('misdirection.jpg');
  41. $blog3->setAuthor($user1);
  42. $blog3->setTags('misdirection, magic, movie, hacking, symblog');
  43. $blog3->setCreated(new \DateTime("2011-07-16 16:14:06"));
  44. $blog3->setUpdated($blog3->getCreated());
  45. $manager->persist($blog3);
  46. $blog4 = new Blog();
  47. $blog4->setTitle('The grid - A digital frontier');
  48. $blog4->setBlog('Lorem commodo. Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque. Nulla consectetur tempus nisl vitae viverra.');
  49. $blog4->setImage('the_grid.jpg');
  50. $blog4->setAuthor($user1);
  51. $blog4->setTags('grid, daftpunk, movie, symblog');
  52. $blog4->setCreated(new \DateTime("2011-06-02 18:54:12"));
  53. $blog4->setUpdated($blog4->getCreated());
  54. $manager->persist($blog4);
  55. $blog5 = new Blog();
  56. $blog5->setTitle('You\'re either a one or a zero. Alive or dead');
  57. $blog5->setBlog('Lorem ipsum dolor sit amet, consectetur adipiscing elittibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque.');
  58. $blog5->setImage('one_or_zero.jpg');
  59. $blog5->setAuthor($user1);
  60. $blog5->setTags('binary, one, zero, alive, dead, !trusting, movie, symblog');
  61. $blog5->setCreated(new \DateTime("2011-04-25 15:34:18"));
  62. $blog5->setUpdated($blog5->getCreated());
  63. $manager->persist($blog5);
  64. $manager->flush();
  65. }
  66. /**
  67. * @return int
  68. */
  69. public function getOrder()
  70. {
  71. return 2;
  72. }
  73. }

执行命令加载测试数据:

  1. $ php app/console doctrine:fixtures:load

现在再起用浏览器打开页面 http://127.0.0.1:8000/1 看看效果。

第5章 Doctrine  - 图2