没有合适的资源?快使用搜索试试~ 我知道了~
首页PHP Doctrine指南:对象持久化实战与技术解密
PHP Doctrine指南:对象持久化实战与技术解密
需积分: 9 22 下载量 93 浏览量
更新于2023-05-31
收藏 1.1MB PDF 举报
"《PHP Persistence: Concepts, Techniques, and Practical Solutions with Doctrine》是一本由Michael Romer撰写的面向经验丰富的PHP程序员和Web开发者的专业书籍。该书旨在帮助读者在面向对象编程的世界中轻松管理关系型数据库,通过 Doctrine 持久化框架实现高效开发。Doctrine 是PHP程序员和Web开发者广泛使用的领先持久化解决方案。 书中内容深入浅出,从基础概念讲起,包括实体(Entities)的定义、实体之间的引用关系,以及如何有效地使用对象关系映射(ORM)技术。读者将学会如何: 1. 定义实体及其之间的关联。 2. 管理实体数据,包括CRUD操作。 3. 掌握 Doctrine 查询语言,编写高效查询。 4. 利用合适的命令行工具进行PHP持久化操作。 5. 应用缓存技术以提高性能。 6. 探索高级主题,如适应不断发展的技术。 作者Michael Romer以其丰富的实践经验,带领读者逐步了解并掌握Doctrine 2的使用方法,适合已经有PHP基础并有一定数据库经验的开发者阅读。本书不仅提供了理论知识,还提供了实际操作指导,确保读者能够在项目中快速上手和实现高效开发。 本书的出版信息包括ISBN-13(平装版:978-1-4842-2558-5,电子版:978-1-4842-2559-2),DOI(10.1007/978-1-4842-2559-2),以及版权和商标注意事项。通过阅读这本书,开发者可以减少编码工作量,提升项目的质量和效率,是PHP开发者数据库驱动应用开发的实用指南。"
资源详情
资源推荐
Chapter 2 ■ IntroduCtIon
6
Our demo domain model is designed to use as many different association types as
possible, so that we can see most of the features of Doctrine 2 in action in the scope of
the demo application. In a real application, the domain model would probably look a bit
different. As you can see, not every association between entities is bidirectional. And as we
will see later in this book, this is an essential feature of an object-relational mapping (ORM)
system such as Doctrine 2 living in the object-oriented world. In relational databases, there
are no unidirectional associations; they are always bidirectional by design.
7
© Michael Romer 2016
M. Romer, PHP Persistence, DOI 10.1007/978-1-4842-2559-2_3
CHAPTER 3
A Self-Made ORM
I believe that showing is always better than telling. Therefore, instead of simply listing the
pros and cons of Doctrine 2, I like to demonstrate what it looks like not having it in your
PHP toolkit when dealing with PHP objects and relational databases.
Jump straight into Doctrine 2? This section illustrates why Doctrine 2 is such a
big help for the application developer. Step by step, we will implement, on our own, certain
ORM features that are already included in Doctrine 2. This chapter is not needed to learn
Doctrine 2, but it helps in understanding why one should learn it.
Loading an Entity
A domain model is a good thing. As long as the application developer acts in the familiar
object-oriented world, there are no obstacles to designing and implementing a domain
model. Design and implementation become harder, though, if it becomes necessary to
persist objects of the domain model in relational databases or to load and reconstruct
previously stored objects. There is, for instance, the fact that objects are more than just
dumb data structures. With their methods, they have behavior as well. Let’s consider the
User entity definition of the Talking demo application:
1 <?php
2 namespace Entity;
3
4 class User
5 {
6 private $id;
7 private $firstName;
8 private $lastName;
9 private $gender;
10 private $namePrefix;
11
12 const GENDER_MALE = 0;
13 const GENDER_FEMALE = 1;
14
CHAPTER 3 ■ A SELF-MADE ORM
8
15 const GENDER_MALE_DISPLAY_VALUE = "Mr.";
16 const GENDER_FEMALE_DISPLAY_VALUE = "Ms.";
17
18 public function assembleDisplayName()
19 {
20 $displayName = '';
21
22 if ($this->gender == self::GENDER_MALE) {
23 $displayName .= self::GENDER_MALE_DISPLAY_VALUE;
24 } else if ($this->gender == self::GENDER_FEMALE) {
25 $displayName .= self::GENDER_FEMALE_DISPLAY_VALUE;
26 }
27
28 if ($this->namePrefix) {
29 $displayName .= ' ' . $this->namePrefix;
30 }
31
32 $displayName .= ' ' . $this->firstName . ' ' . $this->lastName;
33
34 return $displayName;
34 }
36
37 public function setFirstName($firstName)
38 {
39 $this->firstName = $firstName;
40 }
41
42 public function getFirstName()
43 {
44 return $this->firstName;
45 }
46
47 public function setGender($gender)
48 {
49 $this->gender = $gender;
50 }
51
52 public function getGender()
53 {
54 return $this->gender;
55 }
56
57 public function setId($id)
58 {
59 $this->id = $id;
60 }
61
62 public function getId()
CHAPTER 3 ■ A SELF-MADE ORM
9
63 {
64 return $this->id;
65 }
66
67 public function setLastName($lastName)
68 {
69 $this->lastName = $lastName;
70 }
71
72 public function getLastName()
73 {
74 return $this->lastName;
75 }
76
77 public function setNamePrefix($namePrefix)
78 {
79 $this->namePrefix = $namePrefix;
80 }
81
82 public function getNamePrefix()
83 {
84 return $this->namePrefix;
85 }
86 }
Listing 1.1
Interesting here is the method assembleDisplayName(). It creates the “display name” for
a user based on a user’s data. The display name is used to print a post’s author.
1 <?php
2 include('../entity/User.php');
3
4 $user = new Entity\User();
5 $user->setFirstName('Max');
6 $user->setLastName('Mustermann');
7 $user->setGender(0);
8 $user->setNamePrefix('Prof. Dr');
9
10 echo $user->assembleDisplayName();
Listing 1.2
The code in Listing 1.2 results in:
1 Mr. Prof. Dr. Max Mustermann
CHAPTER 3 ■ A SELF-MADE ORM
10
The method assembleDisplayName() therefore defines a specific behavior of a User
object.
If a user’s master data is retrieved from the database, it must be transformed in a way
that allows the behavior described above to be attached to it. In other words, the user’s
master data, retrieved from the database, must be transformed into a User object. Within
the application, we always want to deal with objects of our domain so that we can easily
create a User’s display name by simply calling its assembleDisplayName() method. Let’s
build that into our ORM tool.
First, we set up the database structure:
1 CREATE TABLE users(
2 id int(10) NOT NULL auto_increment,
3 first_name varchar(50) NOT NULL,
4 last_name varchar(50) NOT NULL,
5 gender ENUM('0','1') NOT NULL,
6 name_prefix varchar(50) NOT NULL,
7 PRIMARY KEY (id)
8 );
Then, let’s add dummy data for “Max Mustermann” (the German “John Doe,”
by the way):
1 INSERT INTO users (first_name, last_name, gender, name_prefix)
2 VALUES('Max', 'Mustermann', '0', 'Prof. Dr.');
Now, if we want to reconstruct the User object from the database, we can do it like this:
1 <?php
2 include('../entity/User.php');
3
4 $db = new \PDO('mysql:host=localhost;dbname=app', 'root', '');
5 $userData = $db->query('SELECT * FROM users WHERE id = 1')->fetch();
6
7 $user = new Entity\User();
8 $user->setId($userData['id']);
9 $user->setFirstName($userData['first_name']);
10 $user->setLastName($userData['last_name']);
11 $user->setGender($userData['gender']);
12 $user->setNamePrefix($userData['name_prefix']);
13
14 echo $user->assembleDisplayName();
Database credentials in live systems In a live system, you use strong keywords
and don’t work with user “root,” right? The code shown above is just an example …
剩余117页未读,继续阅读
ramissue
- 粉丝: 354
- 资源: 1488
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- VMP技术解析:Handle块优化与壳模板初始化
- C++ Primer 第四版更新:现代编程风格与标准库
- 计算机系统基础实验:缓冲区溢出攻击(Lab3)
- 中国结算网上业务平台:证券登记操作详解与常见问题
- FPGA驱动的五子棋博弈系统:加速与创新娱乐体验
- 多旋翼飞行器定点位置控制器设计实验
- 基于流量预测与潮汐效应的动态载频优化策略
- SQL练习:查询分析与高级操作
- 海底数据中心散热优化:从MATLAB到动态模拟
- 移动应用作业:MyDiaryBook - Google Material Design 日记APP
- Linux提权技术详解:从内核漏洞到Sudo配置错误
- 93分钟快速入门 LaTeX:从入门到实践
- 5G测试新挑战与罗德与施瓦茨解决方案
- EAS系统性能优化与故障诊断指南
- Java并发编程:JUC核心概念解析与应用
- 数据结构实验报告:基于不同存储结构的线性表和树实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功