4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Symfony4でCRUD

Last updated at Posted at 2019-03-19

Somposerをインストール

$ brew install composer

Symfonyのプロジェクト作成

$ composer create-project symfony/website-skeleton bookshelf
$ cd bookshelf

サーバー起動

$ bin/console server:run

Mysqlの設定(Dockerから)

$ docker run -d -p 3306:3306 \                                                                                                                                                      
    -e MYSQL_USER=db_user \
    -e MYSQL_PASSWORD=db_password \
    -e MYSQL_DATABASE=db_name \
    -e MYSQL_RANDOM_ROOT_PASSWORD=yes \
  mysql:5.7

Doctrineのインストール

$ composer require symfony/orm-pack
$ composer require symfony/maker-bundle --dev

Entityの作成

$ php bin/console make:entity                                                                                                                                                        
 Class name of the entity to create or update (e.g. VictoriousJellybean):
 > Book
 New property name (press <return> to stop adding fields):
 > title
 Field type (enter ? to see all types) [string]:
 > string
 Field length [255]:
 > 255
 Can this field be null in the database (nullable) (yes/no) [no]:
 > no
 updated: src/Entity/Book.php
 Add another property? Enter the property name (or press <return> to stop adding fields):
 > description
 Field type (enter ? to see all types) [string]:
 > text
 Can this field be null in the database (nullable) (yes/no) [no]:
 > no
 updated: src/Entity/Book.php
 Add another property? Enter the property name (or press <return> to stop adding fields):
 > content
 Field type (enter ? to see all types) [string]:
 > longtext
 Field type (enter ? to see all types) [string]:
 > text
 Can this field be null in the database (nullable) (yes/no) [no]:
 > no
 updated: src/Entity/Book.php
 Add another property? Enter the property name (or press <return> to stop adding fields):
 > <return>

マイグレーションの生成

$ php bin/console make:migration

マイグレーションの実行

php bin/console doctrine:migrations:migrate

CRUD

php bin/console make:crud Book

確認

bin/console server:run
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?