(その1から続く)
CLIが動くようになったのでDoctrineのコマンドラインツールが使えるようになりました。このツールは開発においてとても重要な役割を果たすことになります。
モデルのディレクトリ構成
プロジェクトのmodels/配下にDoctrineがモデルクラスを作成するようにします。Doctrine2ではEntity, Proxy, Repositoryという3つのクラス群が必要になります。(これらが何を意味するのか、そしてなぜ必要なのかについては解説の範囲外なので、Doctrine2が標準のORMとして使われているsymfonyな人たちのblogを見てみましょう。また公式サイトのhttp://docs.symfony.gr.jp/symfony2/book/doctrine.html に概略の説明があります。)
さてこれら3つのレイアウトですが、今回はこのように作ることにします。

この構造に合わせてiniファイルを設定していきます。
まずDoctrineがこの構造を読めるようにプロジェクトにpathとnamespaceを追加します。
... includePaths.models = APPLICATION_PATH "/models" ... autoloaderNamespaces[] = Kdf\Entity
生成されるProxyのnamespaceおよびディレクトリを指定してあげる必要があります。
resources.doctrine.orm.entityManagers.default.proxy.namespace = "Kdf\Entity\Proxy" ... resources.doctrine.orm.entityManagers.default.proxy.dir = APPLICATION_PATH "/models/Kdf/Entity/Proxy"
metadata driverのnamespaceおよびマッピングディレクトリも指定します。
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingNamespace = "Kdf\Entity" resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[] = APPLICATION_PATH "/models/Kdf/Entity"
Proxyの自動生成パラメータを開発環境ではONにしておくとProxyが自動で作成されるので開発が楽になります。しかしパフォーマンス的には悪影響を及ぼすので本番環境ではOFFにします。
[production] ... resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = false [development : production] ... resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = true
開発環境用の接続DB情報をdevelopmentセクションに設定します。(※これらを使用してあらかじめMySQLにデータベースおよびユーザを作成して置く必要があります。)
[development : production] ... resources.doctrine.dbal.connections.default.parameters.host = "localhost" resources.doctrine.dbal.connections.default.parameters.dbname = "kdfdb" resources.doctrine.dbal.connections.default.parameters.user = "user" resources.doctrine.dbal.connections.default.parameters.password = "pass"
iniファイルの設定が出来たので、Entityを作ってみます。
application/models/Kdf/Entity/User.php
<?php namespace Kdf\Entity; use Doctrine\ORM\Mapping as ORM; /** * User * * @Table(name="USER") * @Entity(repositoryClass="Kdf\Entity\Repository\UserRepository") */ class User { /** * @var integer $id * * @ORM\Column(name="ID", type="integer") * @Id * @GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var string $userName * * @ORM\Column(name="NAME", type="string", length=50) */ private $name; /** * @var string $password * * @ORM\Column(name="PASSWORD", type="string", length=20) */ private $password; /** * @var datetime $registerDate * * @ORM\Column(name="REGISTER_DATE", type="datetime") */ private $registerDate; /** * @var datetime $updateDate * * @ORM\Column(name="UPDATE_DATE", type="datetime") */ private $updateDate; }
準備が出来たのでDoctrineコマンドを使ってみます。
スキーマを生成します。
php bin/doctrine.php orm:schema-tool:create
ATTENTION: This operation should not be executed in a production environment. Creating database schema... Database schema created successfully!
php bin/doctrine.php orm:generate-entities application/models
このコマンドを実行するとgetter, setterが生成されます。
Processing entity "Kdf\Entity\User" Entity classes generated to "/Path/to/project/application/models"
php bin/doctrine.php orm:generate-proxies
proxyを手動で生成してみます。(生成先のパスは.iniファイルで指定しているので不要です。)
Processing entity "Kdf\Entity\User" Proxy classes generated to "/Path/to/project/application/models/Kdf/Entity/Proxy"
php bin/doctrine.php orm:generate-repositories application/models
repositoryを生成します。
Processing repository "Kdf\Entity\Repository\UserRepository" Repository classes generated to "/Path/to/project/application/models"
まとめ
Doctrineコマンドを使ってEntity, Proxy, Repositoryが生成できるようになりました。今回設定した結果のiniファイルは以下です。
; -----------------------------; forked from https://github.com/guilhermeblanco/ZendFramework1-Doctrine2/blob/master/application/configs/application.ini; -----------------------------
[production]
; --------------------------; PHP Specific Configuration; --------------------------phpSettings.display_startup_errors = 0phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"includePaths.models = APPLICATION_PATH "/models"
; ----------------------------------------; Zend Framework Application Configuration; ----------------------------------------bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"
pluginPaths.Bisna\Application\Resource\ = "Bisna/Application/Resource"
autoloaderNamespaces[] = BisnaautoloaderNamespaces[] = SymfonyautoloaderNamespaces[] = DoctrineautoloaderNamespaces[] = Kdf\Entity
; ------------------------------; Front Controller Configuration; ------------------------------
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 0
; ------------------------------------------------------------------------------; Doctrine Class Loader Configuration; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------; Doctrine Cache Configuration; ------------------------------------------------------------------------------
; Points to default cache instance to be used. Optional is only one cache is definedresources.doctrine.cache.defaultCacheInstance = default
; Cache Instance configuration for "default" cache;resources.doctrine.cache.instances.default.id = defaultresources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\MemcacheCache"
resources.doctrine.cache.instances.default.namespace = "Application_"resources.doctrine.cache.instances.default.options.servers.0.host = localhostresources.doctrine.cache.instances.default.options.servers.0.port = 11211;resources.doctrine.cache.instances.default.options.servers.0.persistent = true;resources.doctrine.cache.instances.default.options.servers.0.weight = 1;resources.doctrine.cache.instances.default.options.servers.0.timeout = 1;resources.doctrine.cache.instances.default.options.servers.0.retryInterval = 15;resources.doctrine.cache.instances.default.options.servers.0.status = true
; ------------------------------------------------------------------------------; Doctrine DBAL Configuration; ------------------------------------------------------------------------------
; Points to default connection to be used. Optional if only one connection is definedresources.doctrine.dbal.defaultConnection = default
; DBAL Connection configuration for "default" connection;resources.doctrine.dbal.connections.default.id = default;resources.doctrine.dbal.connections.default.eventManagerClass = "Doctrine\Common\EventManager";resources.doctrine.dbal.connections.default.eventSubscribers[] = "DoctrineExtensions\Sluggable\SluggableSubscriber";resources.doctrine.dbal.connections.default.configurationClass = "Doctrine\DBAL\Configuration";resources.doctrine.dbal.connections.default.sqlLoggerClass = "Doctrine\DBAL\Logging\EchoSQLLogger";resources.doctrine.dbal.connections.default.types.my_type = "Application\DBAL\Type\MyType"
; Database configuration;resources.doctrine.dbal.connections.default.parameters.wrapperClass = ""resources.doctrine.dbal.connections.default.parameters.driver = "pdo_mysql"resources.doctrine.dbal.connections.default.parameters.dbname = "fmm"resources.doctrine.dbal.connections.default.parameters.host = "localhost"resources.doctrine.dbal.connections.default.parameters.port = 3306resources.doctrine.dbal.connections.default.parameters.charset = "utf8"resources.doctrine.dbal.connections.default.parameters.user = "root"resources.doctrine.dbal.connections.default.parameters.password = "password";resources.doctrine.dbal.connections.default.parameters.driverOptions.ATTR_USE_BUFFERED_QUERIES = true
; ------------------------------------------------------------------------------; Doctrine ORM Configuration; ------------------------------------------------------------------------------
; Points to default EntityManager to be used. Optional if only one EntityManager is definedresources.doctrine.orm.defaultEntityManager = default
; EntityManager configuration for "default" manager;resources.doctrine.orm.entityManagers.default.id = default;resources.doctrine.orm.entityManagers.default.entityManagerClass = "Doctrine\ORM\EntityManager";resources.doctrine.orm.entityManagers.default.configurationClass = "Doctrine\ORM\Configuration";resources.doctrine.orm.entityManagers.default.entityNamespaces.app = "Application\Entity"resources.doctrine.orm.entityManagers.default.connection = defaultresources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = falseresources.doctrine.orm.entityManagers.default.proxy.namespace = "Kdf\Entity\Proxy"resources.doctrine.orm.entityManagers.default.proxy.dir = APPLICATION_PATH "/models/Kdf/Entity/Proxy";resources.doctrine.orm.entityManagers.default.metadataCache = default;resources.doctrine.orm.entityManagers.default.queryCache = default;resources.doctrine.orm.entityManagers.default.resultCache = default;resources.doctrine.orm.entityManagers.default.DQLFunctions.numeric.PI = "DoctrineExtensions\ORM\Query\Functions\Numeric\PiFunction"resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationFiles[] = APPLICATION_PATH "/../library/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php";resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.namespace = "Gedmo";resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.includePath = APPLICATION_PATH "/../library/vendor"resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingNamespace = "Kdf\Entity"resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[] = APPLICATION_PATH "/models/Kdf/Entity"resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderCache = default;resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderNamespaces.App = "Application\DoctrineExtensions\ORM\Mapping"
[staging : production]
[testing : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1
[development : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1resources.frontController.params.displayExceptions = 1
resources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\ArrayCache"resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = true
resources.doctrine.dbal.connections.default.parameters.host = "localhost"resources.doctrine.dbal.connections.default.parameters.dbname = "kdfdb"resources.doctrine.dbal.connections.default.parameters.user = "kdf"resources.doctrine.dbal.connections.default.parameters.password = "kdfpass"その3へ続く
Pingback: Zend Framework1 + Doctrine2 + twigを組み合わせる(その3) | KDF Memo