Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

エンティティ拡張機構の実装 #2267

Merged
merged 8 commits into from
Apr 14, 2017

Conversation

chihiro-adachi
Copy link
Contributor

@chihiro-adachi chihiro-adachi commented Apr 11, 2017

以下を参考にコメントを作成してください。

概要(Overview・Refs Issue)

  • traitでエンティティを拡張する機構を追加

実装サンプル(Productエンティティにカラム追加)

  • app/Acme/Entity/ProductTrait.phpを作成
  • * @EntityExtensionアノテーションで拡張対象のエンティティを指定
  • フィールドに@ORM\Columnなど、Doctrine標準のアノテーションを指定し、カラム追加などが可能。
<?php

namespace Acme\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Annotation\EntityExtension;

/**
  * @EntityExtension("Eccube\Entity\Product")
 */
trait ProductTrait
{
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    public $maker_name;
}
  • Proxyクラスを生成し、doctrineに認識させる(app/cache/doctrine/entity-proxyに出力される)
$ php app/console generate-proxies
gen -> /path/to/ec-cube/app/cache/doctrine/entity-proxies/Product.php
  • doctrineコマンドで、DBへ反映
$ vendor/bin/doctrine  orm:schema-tool:update --dump-sql --force
ALTER TABLE dtb_product ADD maker_name VARCHAR(255) DEFAULT NULL;

Updating database schema...
Database schema updated successfully! "1" queries were executed

実装サンプル(利用側のコード例)

利用側は特に意識する内容はありません。
以下のようなコードで追加したカラムにアクセスすることができます。

  • コントローラ
public function index(Application $app)
{
    $Product = $app['eccube.repository.product']->find(1);
    dump($Product->maker_name);

    $Product->maker_name = 'あああ';
    $app['orm.em']->persist($Product);
    $app['orm.em']->flush();
    ...
  • twig
{{ Product.maker_name }}

方針(Policy)

実装に関する補足(Appendix)

  • app/console generate-proxiesについて
    • Acme/Entity配下のtraitをスキャンし、プロキシを作成します。
    • 出力されるファイルは、本体のエンティティにuse \Acme\Entity\XxxTraitを追加したものです
  • プロキシクラスのオートローディングについて
    • composer.jsonに以下を追加しています。
    • "Eccube\\Entity\\": "app/cache/doctrine/entity-proxies",
    • プロキシクラスが存在する場合はプロキシクラスを読み込みます。

テスト(Test)

  • 以下のケースも検証し、問題なく動作することを確認
    • composer dump-autoload -o の際の挙動
    • apcu利用時の挙動

相談(Discussion)

ref #1984

@chihiro-adachi
Copy link
Contributor Author

todo:proxyクラスの配置先を検討

@chihiro-adachi
Copy link
Contributor Author

todo:コマンドの名称検討

@chihiro-adachi
Copy link
Contributor Author

todo:アノテーション名称を検討

@chihiro-adachi
Copy link
Contributor Author

  • アノテーション名
    • Eccube\Annotation\EntityExtension
  • Proxyの配置先
    • app/proxy/entity
  • コマンド名
    • app/console generate-proxies

chihiro-adachi added 6 commits April 12, 2017 14:22
@chihiro-adachi
Copy link
Contributor Author

@Yangsin
TODO対応しました。

@kiy0taka
アノテーションの読み込みが競合するので、いったんEntityEventアノテーションはsrc/Annotation以下に放り込んでいます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
document Improvements or additions to documentation enhancement 機能追加 experimental
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants