Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

参考ぷるりく

https://github.com/EC-CUBE/ec-cube/pull/80/files

chihiro-adachi
EC-CUBEの開発をしています ※Qiitaへ投稿した記事はすべて私個人としての発言です。
https://github.com/EC-CUBE/ec-cube
ec-cube
日本No.1ECオープンソースのEC-CUBEのコミッターやユーザーのコミュニティです。
http://www.ec-cube.net
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした