エンティティの作成: まず、Doctrineで扱いたいデータベースのテーブルに対応するエンティティクラスを作成します。
php vendor/bin/doctrine orm:generate-entities ./src
Did you mean one of these?
orm:clear-cache:metadata
orm:clear-cache:query
orm:clear-cache:region:collection
orm:clear-cache:region:entity
orm:clear-cache:region:query
orm:clear-cache:result
orm:convert-d1-schema
orm:convert-mapping
orm:convert:d1-schema
orm:convert:mapping
orm:ensure-production-settings
orm:generate-entities
orm:generate-proxies
orm:generate-repositories
orm:generate:entities
orm:generate:proxies
orm:generate:repositories
orm:info
orm:mapping:describe
orm:run-dql
orm:schema-tool:create
orm:schema-tool:drop
orm:schema-tool:update
orm:validate-schema
A project to demonstrate how to integrate Doctrine 2.x into Slim. This is the companion code for the Cookbook entry Using Doctrine with Slim in Slim's documentation.
- PHP 8.1+
- PHP SQLite extension
- Composer
The Slim-Doctrine-Demo project is a small REST API that allows its clients to create and retrieve lists of users.
- GET /api/users -> Retrieves a list of all users created so far.
- POST /api/users -> Creates a new user (does not accept any parameters, fake data is autogenerated).
At its core, Doctrine's EntityManager
is used to persist and retrieve these
users from an SQLite database.
Slim-Doctrine-Demo
├── public
│ └── index.php -- HTTP front controller (requires ../bootstrap.php)
├── src
│ ├── Rest -- Slim request handlers
│ │ ├── CreateUser.php
│ │ └── ListUsers.php
│ ├── DI
│ │ ├── Doctrine.php -- EntityManager service definition
│ │ └── Slim.php -- Slim request handlers service definitions
│ └── Domain -- Annotated entity classes
│ └── User.php
├── tests/ -- Automated tests
├── var
│ ├── coverage/ -- Test coverage results in HTML
│ ├── doctrine/ -- Doctrine metadata cache
│ └── database.sqlite -- Development database
├── bootstrap.php -- DI container setup (requires ./settings.php)
├── cli-config.php -- Configuration file for the vendor/bin/doctrine development tool
├── composer.json
├── composer.lock
├── LICENSE
├── phpunit.xml.dist
├── README.md -- You are here.
├── settings.php -- Settings currently in use (ignored by Git)
└── settings.php.dist -- Sample settings.php for development (committed to Git)
Typing composer serve
in a console will install the project dependencies, create the database and open
the API at http://localhost:8001
. Once it is running you can make requests against it with a browser,
curl or similar tools.
$ curl -X POST localhost:8001/api/users
$ curl -X GET localhost:8001/api/users
% curl -X POST \
-H "Content-Type: application/json" \
--data '{"query": "query { user(id: \"1\") { id email registered_at } }"}' \
http://localhost:8001/graphql
% curl -X POST \
-d '{"query": "query { allUsers { id email registered_at } }" }' \
-H "Content-Type: application/json" \
http://localhost:8001/graphql
% curl -X POST \
-H "Content-Type: application/json" \
--data '{"query": "mutation { createUser(email: \"[email protected]\", password: \"password\") { id email registered_at } }"}' \
http://localhost:8001/graphql
% curl -X POST \
-H "Content-Type: application/json" \
--data '{"query": "mutation { updateUser(id: \"1\", email: \"[email protected]\") { id email registered_at } }"}' \
http://localhost:8001/graphql
% curl -X POST \
-H "Content-Type: application/json" \
--data '{"query": "mutation { deleteUser(id: \"1\") { id email } }"}' \
http://localhost:8001/graphql
Run ./bin/doctrine
(this script needs the project dependencies, so run composer install
first).
Similarly, typing composer test
will take care of loading the testing environment and running PHPUnit. If the XDebug exension is enabled code coverage results will be available at var/coverage/
after running the tests.