本系列是读php data persistence with doctrine2 orm的笔记,本文是第二篇:doctrine的使用
接着上篇我们自己造了个轮子,本篇开始我们还是实现上篇的功能,不过是用Doctrine来实现一遍。
doctrine使用
我们采用yaml的方式来配置Entity的信息,先来个User的配置,文件config/yaml/App.Entity.User.dcm.yml
App\Entity\User:
type: entity
table: users
id:
id:
type: integer
generator:
strategy: AUTO
fields:
firstName:
type: string
column: first_name
lastName:
type: string
column: last_name
gender:
type: smallint
namePrefix:
type: string
column: name_prefix
oneToMany:
posts:
targetEntity: App\Entity\Post
mappedBy: user
此处为了使用doctrine,我们要做一些配置,先来一个初始化文件config/bootstrap.php
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once __DIR__ . '/../vendor/autoload.php';
$isDevMode = true;
$config = Setup::createYAMLMetadataConfiguration( [ __DIR__ . "/../config/yaml" ], $isDevMode );
$dbParams = [
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'root',
'dbname' => 'app',
'port' => 33060,
'host' => '127.0.0.1',
];
$em = EntityManager::create($dbParams, $config);
然后再是一个使用命令行的配置文件cli-config.php
<?php
require_once "./scripts/bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($em);
到这我们就可以通过命令行命令
./vendor/bin/doctrine orm:create
来创建我们的数据库了,而Entity的文件还是上一篇的,此处不再写了,到这,我们就可以创建一些脚本来完成我们的基本操作了。
创建
我们先来完成user的创建工作,脚本如下:
<?php
// create_user.php
require_once __DIR__ . '/bootstrap.php';
if ($argc != 4){
echo "php " . $argv[0] . ' firstName lastName gender' . PHP_EOL;
return;
}
$firstName = $argv[1];
$lastName = $argv[2];
$gender = $argv[3];
$newUser = new \App\Entity\User();
$newUser->setFirstName($firstName);
$newUser->setLastName($lastName);
$newUser->setGender($gender);
$em->persist($newUser);
$em->flush();
echo "Created User with ID " . $newUser->getId() . "\n";
通过命令行php scripts/create_user.php zhuan xu 1
来完成用户的创建。
查询
<?php
// list_users.php
use Illuminate\Support\Collection;
require_once __DIR__ . '/bootstrap.php';
$userRepository = $em->getRepository(\App\Entity\User::class);
$users = $userRepository->findAll();
$users = new Collection($users);
$users->each(function( \App\Entity\User $user){
echo sprintf("- %s\n",$user->assembleDisplayName());
});
上面的操作完成用户的list
<?php
// show_user.php <id>
require_once __DIR__ . '/bootstrap.php';
if ($argc != 2){
echo "php " . $argv[0] . ' id' . PHP_EOL;
return;
}
$id = $argv[1];
$userRepository = $em->getRepository(\App\Entity\User::class);
/** @var \App\Entity\User $user */
$user = $userRepository->find($id);
if ($user === null) {
echo "No user found.\n";
exit(1);
}
echo sprintf("- %s\n", $user->assembleDisplayName());
上面完成用户的查询
更新
<?php
// update_user.php <id> <firstName>
require_once __DIR__ . '/bootstrap.php';
if ($argc != 3){
echo "php " . $argv[0] . ' id firstName' . PHP_EOL;
return;
}
$id = $argv[1];
$firstName = $argv[2];
$userRepository = $em->getRepository(\App\Entity\User::class);
/** @var \App\Entity\User $user */
$user = $userRepository->find($id);
if ($user === null) {
echo "No user found.\n";
exit(1);
}
$user->setFirstName($firstName);
$em->flush();
通过上面的脚本完成更新操作
关联
App\Entity\Post:
type: entity
table: posts
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string
content:
type: string
manyToOne:
user:
targetEntity: App\Entity\User
inversedBy: posts
joinColumn:
name: user_id
referencedColumnName: id
上面定义了User和Post的关联关系,此时通过命令
./vendor/bin/doctrine orm:validate-schema
可以检查我们定义的yaml文件是否正确
通过上面的定义,我们来看下怎么创建一个Post
<?php
// create_post.php <user_id>
require_once __DIR__ . '/bootstrap.php';
if ($argc != 2){
echo "php " . $argv[0] . ' <user_id>' . PHP_EOL;
return;
}
$user_id = $argv[1];
$userRepository = $em->getRepository(\App\Entity\User::class);
/** @var \App\Entity\User $user */
$user = $userRepository->find($user_id);
if ($user === null) {
echo "No user found.\n";
exit(1);
}
$post = new \App\Entity\Post();
$post->setContent("some thing good");
$post->setTitle("good post");
$post->setUser($user);
$em->persist($post);
$em->flush();
echo "Your new Post Id: ".$post->getId()."\n";
至此我们就完成了上一篇介绍的功能。
小结
在深入Doctrine之前,我们先来看下目前为止我们所了解的。Doctrine通过entity manager管理着Entity,所有的查询,更新操作都是通过entity manager完成的,通过entity manager我们获取到某一特定Entity的Repository,通过Repository提供的各种finders来查询Entity。
DBAL(Doctrine’s database access layer)是Doctrine ORM的基础,DBAL通过封装PDO来提供一个更方便的操作接口,而Doctrine ORM则是基于DBAL提供了一个比DBAL更方便的接口,具体Doctrine的实现由机会再深入讲解的。
本文完整的代码可以查看https://github.com/zhuanxuhit/doctrine-learn
网友评论