萌えキャラとは何だったのか

ギークにも絵描きにもなれない者の末路

php入れたのだから早速admingeneratorを試したいのだが、symfony2になってからのadmingeneratorは2種類あるっぽい

どっちがどうとか知らないので、今回はとりあえず AdmingeneratorGeneratorBundle の方を試して行く

インストール

なにはなくともまずプロジェクトを作る

$ php composer.phar create-project symfony/framework-standard-edition 2.3.6 2.3.6

プレーンな状態で動くことを確認

AdmingeneratorGeneratorBundle / Resources / doc / installation / installation-via-composer.mdを参考にインストールを進める こっちは自分には全く役に立たなかった

composer.jsonを修正してupdateする

--- composer.json.1 2013-10-19 16:26:06.000000000 +0900
+++ composer.json   2013-10-19 16:27:02.000000000 +0900
@@ -18,7 +18,8 @@
         "sensio/distribution-bundle": "2.3.*",
         "sensio/framework-extra-bundle": "2.3.*",
         "sensio/generator-bundle": "2.3.*",
-        "incenteev/composer-parameter-handler": "~2.0"
+        "incenteev/composer-parameter-handler": "~2.0",
+        "cedriclombardot/admingenerator-generator-bundle": "2.3.*@dev"
     },
     "scripts": {
         "post-install-cmd": [
@@ -37,7 +38,8 @@
         ]
     },
     "config": {
-        "bin-dir": "bin"
+        "bin-dir": "bin",
+        "component-dir": "web/components"
     },
     "minimum-stability": "stable",
     "extra": {

たぶん怒られるのでさらにcomposer.jsonを修正してupdateする

--- composer.json.2 2013-10-19 16:48:58.000000000 +0900
+++ composer.json   2013-10-19 16:51:56.000000000 +0900
@@ -41,7 +41,7 @@
         "bin-dir": "bin",
         "component-dir": "web/components"
     },
-    "minimum-stability": "stable",
+    "minimum-stability": "dev",
     "extra": {
         "symfony-app-dir": "app",
         "symfony-web-dir": "web",

Symfony\Component\Process\Exception\ProcessTimedOutException などと言って怒られる場合はタイムアウトの時間を伸ばしてみる

$ COMPOSER_PROCESS_TIMEOUT=600 php composer.phar update

上手く言ったらBundleを有効にする

--- app/AppKernel.php.1 2013-10-19 17:26:45.000000000 +0900
+++ app/AppKernel.php   2013-10-19 17:27:12.000000000 +0900
@@ -16,6 +16,9 @@
             new Symfony\Bundle\AsseticBundle\AsseticBundle(),
             new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
             new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
+            new Admingenerator\GeneratorBundle\AdmingeneratorGeneratorBundle(),
+            new Knp\Bundle\MenuBundle\KnpMenuBundle(),
+            new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
         );

         if (in_array($this->getEnvironment(), array('dev', 'test'))) {

app/config/config.yml に admingenerator の設定追加 使うORMとAsseticを使うかで修正内容を変えるっぽい

--- app/config/config.yml.1 2013-10-19 17:28:28.000000000 +0900
+++ app/config/config.yml   2013-10-19 17:32:20.000000000 +0900
@@ -64,3 +64,13 @@
     username:  %mailer_user%
     password:  %mailer_password%
     spool:     { type: memory }
+
+admingenerator_generator:
+    # choose  and enable at least one
+    use_propel:           false
+    use_doctrine_orm:     true
+    use_doctrine_odm:     false
+
+    # choose and uncomment only one
+#    base_admin_template: AdmingeneratorGeneratorBundle::base_admin.html.twig
+    base_admin_template: AdmingeneratorGeneratorBundle::base_admin_assetic_less.html.twig

Asseticは無視して Install assets

$ php app/console assets:install web --symlink

Dashboard route も無視

AdmingeneratorGeneratorBundle のインストール自体は多分これで完了

php app/console generate:bundle する

最初から admin:generate-admin admin:generate-bundle をして admingenerater が適用されたBundleを作っても良いのだが色々面倒だったので

  • Bundleを作る
  • Entityを作る
  • admin:generate-admin する

という感じで進める

$ php app/console generate:bundle
  • Bundle namespace: Acme/SampleBundle
  • Bundle name [AcmeSampleBundle]:
  • Target directory [/project/path/src]:
  • Configuration format (yml, xml, php, or annotation): annotation
  • Do you want to generate the whole directory structure [no]?
  • Do you confirm generation [yes]?
  • Confirm automatic update of your Kernel [yes]?
  • Confirm automatic update of the Routing [yes]?

データベースと Doctrine (“The Model”) ここを参考に適当にEntityを作って行く

確認ごときに mysql は仰々しすぎるので sqlite にする

--- app/config/parameters.yml.1 2013-10-19 19:39:04.000000000 +0900
+++ app/config/parameters.yml   2013-10-19 19:39:25.000000000 +0900
@@ -1,6 +1,6 @@
 # This file is auto-generated during the composer install
 parameters:
-    database_driver: pdo_mysql
+    database_driver: pdo_sqlite
     database_host: 127.0.0.1
     database_port: null
     database_name: symfony
@@ -10,5 +10,5 @@
     mailer_host: 127.0.0.1
     mailer_user: null
     mailer_password: null
-    locale: en
+    locale: ja
     secret: ThisTokenIsNotSoSecretChangeIt

この状態で doctrine:database:create するとプロジェクト直下に "symfony" とか出来て悲しみを背負う

よく見ると config.yml に「sqlite では設定を追加しろ」という指示が書いてあるので従う

--- app/config/config.yml.2 2013-10-19 19:43:37.000000000 +0900
+++ app/config/config.yml   2013-10-19 19:43:50.000000000 +0900
@@ -51,7 +51,7 @@
         charset:  UTF8
         # if using pdo_sqlite as your database driver, add the path in parameters.yml
         # e.g. database_path: %kernel.root_dir%/data/data.db3
-        # path:     %database_path%
+        path:     %database_path%

     orm:
         auto_generate_proxy_classes: %kernel.debug%

--- app/config/parameters.yml.2 2013-10-19 19:43:15.000000000 +0900
+++ app/config/parameters.yml   2013-10-19 19:44:06.000000000 +0900
@@ -12,3 +12,4 @@
     mailer_password: null
     locale: ja
     secret: ThisTokenIsNotSoSecretChangeIt
+    database_path: %kernel.root_dir%/data/data.db3

この状態で doctrine:database:create すると今度はパスが無くてdbファイルが作れないのでディレクトリを掘る

$ mkdir app/data

これでようやく doctrine:database:create が出来る

Entity を作る

ドキュメントでは手で作成してたりするが面倒なのでTipに書いてあるコマンドで作る

$ php app/console doctrine:generate:entity --entity="AcmeSampleBundle:Product" --fields="name:string(255) price:float description:text"
  • The Entity shortcut name [AcmeSampleBundle:Product]:
  • Configuration format (yml, xml, php, or annotation) [annotation]: annotation
  • New field name (press to stop adding fields):
  • Do you want to generate an empty repository class [no]?
  • Do you confirm generation [yes]?

おそらくこれでEntityは出来たので admin:generate-admin してみる

$ php app/console admin:generate-admin
  • Generator to use (doctrine, doctrine_odm, propel) [doctrine]:
  • Bundle namespace: Acme/SampleBundle
  • Model name [YourModel]: Product
  • Bundle name [AcmeSampleBundle]:
  • Target directory [/project/path/src]:
  • Prefix of yaml: sample
  • Do you confirm generation [yes]?
  • Confirm automatic update of the Routing [yes]?

これで諸々最初に必要なものが出来たハズなので http://host:port/app_dev.php/admin/acme_sample_bundle/sample/ を確認するとテーブルが無いと言って怒られるので作る

$ php app/console doctrine:schema:create

echo ".schema" | sqlite3 app/data/data.db3 とかして出来てるのを確認

これで再度 http://host:port/app_dev.php/admin/acme_sample_bundle/sample/ を確認するとそれっぽい画面が出てくる

触ってみる

とりあえずレコード追加してみるかと触ると setId()ねぇよ 的な怒られ方をする

Productの中を見ると確かに無いがこれはAutoIncrementが効いているからなので、むしろcreate,edit時に編集させないようにするのが正しいと思われる

--- src/Acme/SampleBundle/Resources/config/Sample-generator.yml.1   2013-10-19 20:24:37.000000000 +0900
+++ src/Acme/SampleBundle/Resources/config/Sample-generator.yml 2013-10-19 20:25:10.000000000 +0900
@@ -26,14 +26,14 @@
     new:
         params:
             title: New object for SampleBundle
-            display: ~
+            display: [name, price, description]
             actions:
                 save: ~
                 list: ~
     edit:
         params:
             title: "You're editing the object \"%object%\"|{ %object%: Product.title }|"
-            display: ~
+            display: [name, price, description]
             actions:
                 save: ~
                 list: ~

こんな感じで generator.yml を修正してもう一度試すと今度は title なんてメソッドねぇよ 的な怒られ方をする

レコードは追加されていてレコード追加後 edit へリダイレクトした後にテンプレートのほうで怒られている

これは generator.yml タイトル表示にtitleを参照しようとしてるからなので適当に name に変える

--- src/Acme/SampleBundle/Resources/config/Sample-generator.yml.2   2013-10-19 20:33:43.000000000 +0900
+++ src/Acme/SampleBundle/Resources/config/Sample-generator.yml 2013-10-19 20:34:02.000000000 +0900
@@ -32,14 +32,14 @@
                 list: ~
     edit:
         params:
-            title: "You're editing the object \"%object%\"|{ %object%: Product.title }|"
+            title: "You're editing the object \"%object%\"|{ %object%: Product.name }|"
             display: [name, price, description]
             actions:
                 save: ~
                 list: ~
     show:
         params:
-            title: "You're viewing the object \"%object%\"|{ %object%: Product.title }|"
+            title: "You're viewing the object \"%object%\"|{ %object%: Product.name }|"
             display: ~
             actions:
                 list: ~

これでとりあえず触れる状態になったように思う

今回はここまで

次ぎはリレーション作ったときにどうなるのか、色々変えたくなったときにどうしたらいいのかを見たいのとsonataの方も試してみたいと思う