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

エンティティ継承機能を追加 #1817

Closed
wants to merge 7 commits into from

Conversation

izayoi256
Copy link
Contributor

@izayoi256 izayoi256 commented Oct 6, 2016

機能概要

本体やプラグインのエンティティの継承が可能になります。

説明

次のリポジトリを参照して下さい。 https://github.com/izayoi256/ExtendEntity
本体で継承する場合はextended_entities.yml.distに継承元エンティティを列挙して下さい。

@Yangsin Yangsin added this to the 3.0.x milestone Oct 13, 2016
@Yangsin
Copy link

Yangsin commented Oct 13, 2016

ありがとうございます。
プラグインによる拡張性や開発効率のアップが期待できそうなきもしますが、不具合調査などで混乱をきたしそうなきもします。
他の方のご意見も伺いたいので、discussion にさせていただきしばらくおかせてもらいますね。
マイルストーンはいったん3.0.xとしておきます。

@nanasess
Copy link
Contributor

Association Override という方法もあるようです。
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#overrides

もうちょっと簡潔に書けたら嬉しいんですけどね 😟

@ttsuru
Copy link
Contributor

ttsuru commented Jan 23, 2017

方向としては何かしらこういうのできるといいですよね!

@izayoi256

    /*
     * 以下、エンティティ拡張機構
     *
     * Copyright (c) by Paulius Jarmalavicius
     * Released under the MIT license
     * https://opensource.org/licenses/mit-license.php
     */

となっていますが、どこかからの流用になるのでしょうか?

@ttsuru
Copy link
Contributor

ttsuru commented Jan 23, 2017

https://github.com/pjarmalavicius/PjEntityExtendBundle

こちらがベースですか?Symfonyでもこんなのあったんですね。

@izayoi256
Copy link
Contributor Author

@ttsuru
そうですね、そちらを参考にさせていただきました。

@nanasess
最初はAssociation Overrideも模索していたんですが、何かの理由でこちらの方法で実装した記憶があります。実現できなかったのか、本体への影響を懸念したのか…。こういうのは逐一メモしておかないといけませんね。

@nanasess
Copy link
Contributor

Association Override だと、純粋にフィールドの追加ができないですね。たぶん、そういうポリシーなんでしょう。。。

こんなクラスを app/Eccube/Entity に作って、

namespace Eccube2\Entity;

/**
 * Product の拡張
 * @Entity(repositoryClass="Eccube2\Repository\ProductRepository")
 */
class Product extends \Eccube\Entity\Product
{
    /**
     * @Column(name="extended_parameter", type="string")
     */
    protected $extendedParameter;
}

以下のように schema-tool 叩いたら、 dtb_product に extended_parameter が追加される、とかできたらいいなぁと思って模索してるんですけどね。

php vendor/bin/doctrine orm:schema-tool:update --force

継承ではなく、trait を使って、柔軟に拡張できたらもっと嬉しいんですけど。
ClassMetadata と睨めっこしていますが、なかなかハードルが高そうです 😟

@nanasess
Copy link
Contributor

https://github.com/izayoi256/ExtendEntity を試してみましたが、 orm:validate-schema で MappingException になってしまいました。

$ vendor/bin/doctrine orm:validate-schema

  [Doctrine\ORM\Mapping\MappingException]
  Duplicate definition of column 'product_id' on entity 'Plugin\ExtendEntity\
  Entity\Product' in a field or discriminator column mapping.

Inheritance Mapping などの正攻法でやるか、 フィールド名で正規化する等の対応が必要かもしれません 😟

@izayoi256
Copy link
Contributor Author

@nanasess
orm:validate-schemaを知らなかったので助かります。ありがとうございます。今回の実装は裏技すぎましたね。
こういった機能があれば便利だとは思うので、今後の開発にお役立ていただければと思います。

@nanasess
Copy link
Contributor

一旦 ClassMetadata を Yaml 形式に吐き出して、 array_replace_recursive() などでマージした ClassMetadata を読み込ませるようにしてやれば Duplicate definition な問題も解決できそうです。

@nanasess
Copy link
Contributor

Inheritance Mapping を使えば、 Doctrine の標準機能だけで継承を実現できそうです。
(Doctrine2.4 では試せていませんが...)

nanasess@d5dccd0#diff-fe39fda74ed66678cbb07ae2de9e1dd0

@t-nagahashi t-nagahashi modified the milestones: 3.0.15, 3.0.x Mar 15, 2017
@ryo-endo
Copy link
Contributor

@izayoi256
変更量が多く、影響も読み切れないため、マイナーバージョンアップでの取り込みは見送らせていただきます。

EC-CUBE 3.1に向けては、
#2127 で「Inheritance Mapping の採用」を検討しておりますので、こちらにご意見いただければと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants