1
0

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 1 year has passed since last update.

【Symfony】CRUDをコマンドで作成する

Posted at

概要

ControllerやCommand,Entityを自動的に作ってくれるコマンドを使えるようにするには
以下のコマンドをプロジェクトのルートディレクトリ上で実行する

terminal
composer require --dev symfony/maker-bundle

makerBundleによって追加されるCommand

  • make:auth Creates a Guard authenticator of different flavors
    Guard authenticatorを作る(多分新しい権限を作るってことかな?)
  • make:command Creates a new console command class
    新しくコマンドを作る
  • make:controller Creates a new controller class
    コントローラーを作る
  • make:crud Creates CRUD for Doctrine entity class
    クラッドを作る
  • make:docker:database Adds a database container to your docker-compose.yaml file
    データベースをdocker.yamlに入れる
  • make:entity Creates or updates a Doctrine entity class, and optionally an API Platform resource
    エンティティを作る(カラムの名前、データ型、文字数なども設定できました)
  • make:fixtures Creates a new class to load Doctrine fixtures
    よくわからない
  • make:form Creates a new form class
    フォームを作る
  • make:message Creates a new message and handler
    メッセージを作る
  • make:messenger-middleware Creates a new messenger middleware
    メッセンジャーのミドルウェアを作る
  • make:migration Creates a new migration based on database changes
    データベースの変更に基づきマイグレーションを作る
  • make:registration-form Creates a new registration form system
    登録フォームシステムを作る
  • make:reset-password Create controller, entity, and repositories for use with symfonycasts/reset-password-bundle
    パスワードをリセットするためのコントローラー、エンティティ、リポジトリーを作る
  • make:serializer:encoder Creates a new serializer encoder class
  • make:serializer:normalizer Creates a new serializer normalizer class
  • make:stimulus-controller Creates a new Stimulus controller
  • make:subscriber Creates a new event subscriber class
  • make:test [make:unit-test|make:functional-test] Creates a new test class
    テストを作る
  • make:twig-component Creates a twig (or live) component
    twigの部品を作る
  • make:twig-extension Creates a new Twig extension with its runtime class
    twigの拡張を作る
  • make:user Creates a new security user class
    セキュアなユーザークラスを作る
  • make:validator Creates a new validator and constraint class
    バリデーターを作る
  • make:voter Creates a new security voter class

よくわからないやつも何個かありますが、結構使えそうな印象

使用例

entityの作成

bin/console make:entity Tweet

 New property name (press <return> to stop adding fields):
 > post

 Field type (enter ? to see all types) [string]:
 > 

 Field length [255]:
 > 

 Can this field be null in the database (nullable) (yes/no) [no]:
 > 

 updated: src/Entity/Tweet.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > 

migrationファイルを作成し、データベーススキーマを更新

bin/console make:migration
bin/console doctrine:migrations:migrate

CRUD作成

php bin/console make:crud
The class name of the entity to create CRUD (e.g. VictoriousChef):
 > Tweet

 Choose a name for your controller class (e.g. UserController) [UserController]:
 > 

 Do you want to generate tests for the controller?. [Experimental] (yes/no) [no]:
 > 

最後に

ページを実際に見てみると簡易的なCRUDが実装されていて、データベースとの連携もできていたので、
railsみたいにさくっと実装ができて、とても楽しかったです。
改修案件などはこのコマンド使いにくいかもしれませんが、新規案件などで最初スピーディーにプロジェクト作りたい時は
かなり時間短縮になると思います。

参考にしたサイト=>https://tech.quartetcom.co.jp/

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?