1

クリーンな 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 ); }

4

2 に答える 2

0

このようなタスクにはArray Collectionを使用します。このオブジェクトには、clean()、contains() などの非常に便利なさまざまなメソッドがあります。

oneToOne、oneToMany などの関係を arrayCollections に格納する傾向があります。

エンティティ内で、コンストラクターを使用してコレクションを開始する必要があります。

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\OneToMany(targetEntity="yourModule\Entity\yourEntity", mappedBy="yourRelation", cascade={"persist"})
 */
protected $yourCollection;

public function __construct()
{
   $this->yourCollection = new ArrayCollection();
}
//once I have the collection I usually create methods like add, remove, etc methods like so:
public function addToCollection($toAdd) {
   $this->yourCollection->add($toAdd);
}

public function removeFromCollection($toRemove) {
   $this->yourCollection->removeElement($toRemove);
}

//etc.....
于 2013-10-24T07:53:21.900 に答える