LoginSignup
2

More than 3 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

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
What you can do with signing up
2