クリーンな OOP と MVC 構造を作成し、モデルに EntityManager へのアクセスを提供しながら、ZF2 と Doctrine を使用してビジネス ロジック モデルを実装する「正しい方法」とは何ですか?
私はすべてのビジネス ロジックに Doctrine リポジトリを使用したくなりますが、主に複雑なクエリを実行するために使用する必要があることはわかっています。また、ZF2 のモデルにいくつかの方法で依存関係 (EntityManager) を提供できることも知っています。 オブジェクトからコントローラーへ、またはサービスロケーターからオブジェクトを取得-td4656872.html
実際の問題を説明するために、単純な Web ショップ機能のモデル レイヤーをどのように作成するかを説明します。
まず、エンティティがあります
/**
* @Entity(repositoryClass="App\Repository\ProductRepository")
* @Table(name="products")
*/
class Product { ... }
/**
* @Entity(repositoryClass="App\Repository\ProductgroupRepository")
* @Table(name="productgroups")
*/
class Productgroup { ... }
そして、より詳細なクエリ機能を提供するリポジトリ
class ProductRepository extends EntityRepository {
public function isAvailable() { ... }
public function getQuantity() { ... }
public function getProductImages() { ... }
public function getRelatedProducts() { ... }
...
}
class ProductgroupRepository extends EntityRepository {
public function getAvailableProducts() { ... }
public function getAllProducts() { ... }
...
}
しかし、このようなビジネス ロジックをどこに配置すればよいのでしょうか。
Products functionalities:
- createNewProduct( $data ), eg. update prices, retrieve new quantities, generate a new image gallery, notify the administrator, ...
- deleteProduct(), eg. delete corresponding files and existing associations, put product into archive
- ...
Productgroups functionalities:
- removeProductFromProductgroup( $product), eg. update ordering in group, remove associations, ...
- addProductToProductgroup( $product ), eg. update ordering in group, create associations, ...
- deleteProductgroup(), eg. delete productgroup, set all it's products as uncategorized
- ...
たとえば、サービス レベルで EntityManager が挿入されるクラスとして、Productgroup ビジネス モデルを作成する必要がありますか? --
class Productgroup implements ServiceLocatorAwareInterface
{
public function removeProductFromProductgroup( $productgroup, $product) { }
public function addProductToProductgroup( $productgroup, $product) { }
}
それとも、元のエンティティを拡張して、その内部構造にアクセスできるようにする必要がありますか? --
class Productgroup extends \Application\Entity\Productgroup
implements ServiceLocatorAwareInterface
{
public function removeProductFromProductgroup( $productgroup, $product) { }
public function addProductToProductgroup( $productgroup, $product) { }
}
もしそうなら、ある種の状態設定メソッドも持つべきですか?public function set( $product ) { $this->populate( $product ); }