LoginSignup
11
12

More than 5 years have passed since last update.

データベースからエンティティを生成する

Last updated at Posted at 2015-03-30

doctrine orm でエンティティのクラスをDBから生成する際のメモ

参考

doctrineの公式ドキュメントが以下
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/tools.html

使えるコマンド一覧

$ cd ec-cube
$ vendor/bin/doctrine list

Available commands:
 help                             Displays help for a command
 list                             Lists commands
dbal
 dbal:import                      Import SQL file(s) directly to Database.
 dbal:run-sql                     Executes arbitrary SQL directly from the command line.
orm
 orm:clear-cache:metadata         Clear all metadata cache of the various cache drivers.
 orm:clear-cache:query            Clear all query cache of the various cache drivers.
 orm:clear-cache:result           Clear all result cache of the various cache drivers.
 orm:convert-d1-schema            Converts Doctrine 1.X schema into a Doctrine 2.X schema.
 orm:convert-mapping              Convert mapping information between supported formats.
 orm:convert:d1-schema            Converts Doctrine 1.X schema into a Doctrine 2.X schema.
 orm:convert:mapping              Convert mapping information between supported formats.
 orm:ensure-production-settings   Verify that Doctrine is properly configured for a production environment.
 orm:generate-entities            Generate entity classes and method stubs from your mapping information.
 orm:generate-proxies             Generates proxy classes for entity classes.
 orm:generate-repositories        Generate repository classes from your mapping information.
.
.
.

listオプションで利用出来るコマンドができる 

orm:convert:mapping, orm:generate:entitiesがそれぞれ該当するのコマンド

orm:convert:mapping

vendor/bin/doctrine orm:convert:mapping --namespace="Eccube\Entity\\" --from-database yml src/Eccube/Resource/doctrine

上記でsrc/Eccube/Resource/doctrine以下にorm.ymlを生成してくれる

--from-database でDBのスキーマをymlに落とす
--namespaceでnamespaceつける

※--from-databaseで参照する接続先は、app/config/eccube/config.ymlに定義されているDB情報をみる
※リレーションは手動で定義する必要がある。外部キー張っていれば勝手にやってくれる?

orm:genereta:entities

vendor/bin/doctrine orm:generate:entities --extend="Eccube\\Entity\\AbstractEntity" src/

--extendでベースクラス継承したエンティティも作れる

その他

リレーション貼るときは、orm.ymlに以下のように定義してからエンティティ作る

    oneToMany:
        Delivtimes:
            targetEntity: Eccube\Entity\Delivtime
            mappedBy: Delivtime

    oneToMany:
        PaymentOptions:
            targetEntity: Eccube\Entity\PaymentOption
            mappedBy: PaymentOption
    oneToMany:
        Delivtimes:
            targetEntity: Eccube\Entity\Delivtime
            mappedBy: Delivtime

レポジトリ作る場合は、orm.ymlに定義して、generate:repositoriesする

repositoryClass: Eccube\Repository\XXXRepository
vendor/bin/doctrine orm:generate:repositories src/Eccube/Repository

参考ぷるりく

11
12
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
11
12