Come to this talk prepared to learn about the Doctrine PHP open source project. The Doctrine project has been around for over a decade and has evolved from database abstraction software that dates back to the PEAR days. The packages provided by the Doctrine project have been downloaded almost 500 million times from packagist. In this talk we will take you through how to get started with Doctrine and how to take advantage of some of the more advanced features.
2. Who am I?
- CTO of MoreCommerce.com
- Using PHP for 15 years
- Involved with Doctrine and
Symfony development for 10
years
3. What is MoreCommerce?
- E-commerce seller tools and
consumer marketplaces.
- Wholly owned subsidiary of
Alibaba Group
- 100,000 Sellers
- 80M Active Shoppers
- Billions in GMV runs through
MoreCommerce platforms
4. ● Open source PHP project started in 2006
● Initially only an Object Relational Mapper
● Evolved to become a collection of high
quality PHP packages focused on
databases and persistence related
functionality
What is Doctrine?
14. $ php bin/console doctrine:migrations:generate
A database migration is an incremental, reversible change to a relational
database schema.
You define the change to your database schema in a PHP class. Use the
doctrine:migrations:generate command to generate a new blank migration.
What is a Database Migration?
15. Generated Database Migration
final class Version20181012002437 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
}
}
16. Write Your Migration
public function up(Schema $schema) : void
{
$usersTable = $schema->createTable('user');
$usersTable->addColumn('id', 'integer', ['autoincrement' => true, 'notnull' => true]);
$usersTable->addColumn('username', 'string', ['length' => 255]);
$usersTable->setPrimaryKey(['id']);
}
public function down(Schema $schema) : void
{
$schema->dropTable('user');
}
17. Write Your Migration
public function up(Schema $schema) : void
{
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL,
username VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET
utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
}
public function down(Schema $schema) : void
{
$this->addSql('DROP TABLE user');
}
18. $ php bin/console doctrine:migrations:migrate
Application Migrations
WARNING! You are about to execute a database migration that could result in schema
changes and data loss. Are you sure you wish to continue? (y/n)y
Migrating up to 20181012002437 from 0
++ migrating 20181012002437
-> CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(255) NOT
NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE
= InnoDB
++ migrated (0.04s)
------------------------
++ finished in 0.04s
++ 1 migrations executed
++ 1 sql queries
Run Your Migration
19. $ php bin/console doctrine:migrations:migrate prev
Application Migrations
WARNING! You are about to execute a database migration that could result in schema
changes and data loss. Are you sure you wish to continue? (y/n)y
Migrating down to 0 from 20181012002437
-- reverting 20181012002437
-> DROP TABLE user
-- reverted (0.05s)
------------------------
++ finished in 0.05s
++ 1 migrations executed
++ 1 sql queries
Revert Your Migration
20. $ php bin/console make:controller UsersController
Generate Controller
We need a place to play around so let’s use the MakerBundle to generate a
UsersController
21. AppControllerUsersController
class UsersController extends AbstractController
{
/**
* @Route("/users", name="users")
*/
public function index()
{
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/UsersController.php',
]);
}
}
30. $ php bin/console make:entity User
Generating a User Entity
Generate a User entity with property named username that is a string and
has a max length of 255 characters.
Generates the following files/classes:
- src/Entity/User.php (AppEntityUser)
- src/Repository/UserRepository.php (AppRepositoryUserRepository)
33. Inject EntityManager
/**
* @Route("/users/create/{username}", name="users_create")
*/
public function create(EntityManagerInterface $em, Request $request)
{
$user = new User();
$user->setUsername($request->request->get('username'));
$em->persist($user);
$em->flush();
// ...
}
34. persist() and flush()
- persist() - schedules an object to be tracked by Doctrine
- flush() - calculates changes made to objects and commits
the changes to the database with SQL INSERT, UPDATE
and DELETE queries.
35. How does flush() work?
$trackedObjects = getTrackedObjects();
$changesets = [];
foreach ($trackedObjects as $trackedObject)
{
$oldValues = getOldValues($trackedObject);
$newValues = getNewValues($trackedObject);
$changesets[] = calculateChangeset($oldValues, $newValues);
}
commitChangesets($changesets);
37. AppRepositoryUserRepository
class UserRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, User::class);
}
/*
public function findOneBySomeField($value): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
38. Inject the UserRepository
/**
* @Route("/users", name="users")
*/
public function users(UserRepository $userRepository)
{
$users = $userRepository->findAll();
return $this->json(array_map(function(User $user) {
return [
'username' => $user->getUsername(),
];
}, $users));
}
39. Entity Repository: Magic Methods
$user = $userRepository->findOneByUsername('jwage');
$user = $userRepository->findByFieldName('value');
Magic methods are implemented using __call(). When a method that does
not exist is called, the method name is parsed and a query is generated,
executed, and the results are returned.
43. $ php bin/console make:entity User
Modify Your Entity
Modify your User entity and add a property named twitter that is a string and
has a max length of 255 characters.
44. Modify Your Entity
class User
{
// ...
/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private $twitter;
// ...
}
45. $ php bin/console make:migration
Generate Migration
Now that we’ve modified our User class and mapped a new twitter property,
when we run make:migration, a migration will be generated with the SQL
necessary to add the twitter column to the user table.
public function up(Schema $schema) : void
{
$this->addSql('ALTER TABLE user ADD twitter VARCHAR(255) DEFAULT
NULL');
}
46. $ php bin/console doctrine:migrations:migrate
Application Migrations
WARNING! You are about to execute a database migration that could result in
schema changes and data loss. Are you sure you wish to continue? (y/n)y
Migrating up to 20181012041720 from 20181012002437
++ migrating 20181012041720
-> ALTER TABLE user ADD twitter VARCHAR(255) DEFAULT NULL
++ migrated (0.07s)
------------------------
++ finished in 0.07s
++ 1 migrations executed
++ 1 sql queries
Run Your Migration
57. When To Use Transactions
When you have a unit of work that needs to be
ALL OR NOTHING. Meaning, if one part of a
larger process fails, all changes made to the
database are rolled back.
58. $connection->beginTransaction();
try {
$connection->executeQuery('UPDATE users SET twitter = :twitter WHERE username = :username', [
'twitter' => 'jwage',
'username' => 'jwage'
]);
// execute other updates
// do something that throws an Exception and both updates will be rolled back
$connection->commit();
} catch (Exception $e) {
$connection->rollBack();
throw $e;
}
Example Transaction
61. DQL: Doctrine Query Language
Query language similar to SQL except in DQL
you think in terms of your mapped entities and
class properties instead of tables and columns.
The DQL language is parsed and transformed
to platform specific SQL queries.
What is it?
63. DQL -> SQL
SELECT u FROM AppEntitiesUser u WHERE u.status = :status
SELECT u0_.id AS id_0, u0_.username AS username_1,
u0_.twitter AS twitter_2 FROM user u0_ WHERE u0_.username =
?
DQL
SQL
64. Writing DQL Manually
$query = $entityManager->createQuery(
'SELECT u FROM AppEntityUser u WHERE u.username = :username'
);
/** @var User[] $users */
$users = $query->execute([
'username' => 'jon',
]);
66. Advanced DQL Examples
SELECT u FROM User u WHERE u.phonenumbers IS EMPTY
SELECT u FROM User u WHERE SIZE(u.phonenumbers) > 1
SELECT u.id FROM User u WHERE :groupId MEMBER OF
u.groups
SELECT u.id FROM User u WHERE EXISTS (SELECT
p.phonenumber FROM Phonenumber p WHERE p.user = u.id)
67. DQL: Data Transfer Objects
class CustomerDTO
{
public function __construct($name, $email, $city, $value = null)
{
// Bind values to the object properties.
}
}
$query = $em->createQuery('SELECT NEW CustomerDTO(c.name, e.email, a.city)
FROM Customer c JOIN c.email e JOIN c.address a');
/** @var CustomerDTO[] $users */
$users = $query->getResult();
68. Native Raw SQL
use DoctrineORMEntityManagerInterface;
$rsm = new ResultSetMapping();
$rsm->addEntityResult(User::class, 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('u', 'username', 'username');
$rsm->addFieldResult('u', 'twitter', 'twitter');
$query = $em->createNativeQuery(
'SELECT id, username, twitter FROM user WHERE username = :username', $rsm
);
$query->setParameter('username', 'ryan');
/** @var User[] $user */
$users = $query->getResult();