加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

将业务逻辑与PHP Doctrine 2分开

发布时间:2020-12-13 17:58:46 所属栏目:PHP教程 来源:网络整理
导读:我使用symfony 2.3和php doctrine 2. 该计划有以下型号: 实体订单 – 典型的客户订单 entity BadOrderEntry(fields:id,order – 与Order的单向一对一关系,createdAt) 工厂BadOrderEntryFactory用于创建实体BadOrderEntry 存储库BadOrderEntryRepository,用
我使用symfony 2.3和php doctrine 2.

该计划有以下型号:

>实体订单 – 典型的客户订单
> entity BadOrderEntry(fields:id,order – 与Order的单向一对一关系,createdAt)
>工厂BadOrderEntryFactory用于创建实体BadOrderEntry
>存储库BadOrderEntryRepository,用于实体BadOrderEntry的搜索方法
> manager BadOrderEntryManager用于保存/编辑/删除实体BadOrderEntry的方法
> AND MAIN CLASS BadOrderList – 错误订单列表,此类代码:

private $factory;
private $repository;
private $manager;

public function __construct(
    BadOrderEntryFactory $f,BadOrderEntryRepository $r,BadOrderEntryManager $m
) {
    $this->factory = $f;
    $this->repository = $r;
    $this->manager = $m;
}

public function has(Order $order)
{
    return $this->repository->existsByOrder($order);
}

public function add(Order $order)
{
    if (! $this->has($order)) {
        $entry = $this->factory->create($order);
        $this->manager->save($entry);
    }
}

public function remove(Order $order)
{
    $entry = $this->repository->findOneByOrder($order);
    if ($entry !== null) {
        $this->manager->delete($entry);
    }
}

我真的很喜欢这门课的设计.我想了很多.
一切都很美好.但!有一个问题:必须在事务中执行方法添加和删除操作.

PHP Docrine 2中的事务代码如下所示:

<?php
$em->getConnection()->beginTransaction();
try {
    //... do some work
    $em->getConnection()->commit();
} catch (Exception $e) {
    $em->getConnection()->rollback();
    throw $e;
}

但是如何在BadOrderList中调用此代码?

我花了很多时间并根据数据库(相应的PHP Doctrine 2)删除,并再次创建它?
现在依赖是隐藏在类BadOrderEntryRepository和BadOrderEntryManager中.

如何在类BadOrderList中隐藏对事务机制的依赖?

在我们讨论之后,我会回答你的问题.
问题实际上不是“如何隐藏类BadOrderList中对事务机制的依赖?”,而是如何将模型与持久层分离? (在特定情况下的Doctrine2).

我尝试用一??些代码来说明我的建议

class BadOrderEntry
// Bad - is too bad word to describe an order here. Why is it bad? Is it Declined? Cancelled?
{
   private $order;
   // some code
}
class BadOrderEntryFactory 
{ 
   // If there is not to much processing to build BadOrderEntry better use factory method like BadOrderEntry::fromOrder($order); 
}
class BadOrderEntryRepository 
{ 
   // here is some read model 
}
class BadOrderEntryManager  
// ITS a part of our model and shouldn't be coupled to ORM
{
  public function save(BadEntry $be) 
  {
    // some model events,model actions
    $this->doSave($be); // here we should hide our storage manipulation
  }

  protected function doSave($be) // it can be abstract,but may contain some basic storage actions  
  { 
  }

  // similar code for delete/remove and other model code
}
class ORMBadOrderEntryManager extends BadOrderEntryManager 
// IT'S NOT the part of your model. There is no business logic. There is only persistent logic and transaction manipulation
{ 
  protected $entityManager;

  // some constructor to inject doctrine entitymanager

  protected doSave($be)
  {
    $em = $this->entityManager;
    $em->getConnection()->beginTransaction(); // suspend auto-commit
    try {
      $em->persist($be);
      $em->flush();
      $em->getConnection()->commit();
    } catch (Exception $e) {
      $em->getConnection()->rollback();
      throw $e;
    }
  }
}
// You can also implement ODMBadOrderEntryManager,MemcacheBadOrderEntryManager etc.

因此,如果我们谈论目录结构,您的所有模型都可以移出捆绑并在任何地方使用.你的Bundle结构如下:

BadEntryBundle
|
+ Entity
| |
| --- BadOrderEntryEntity.php
|
+ ORM
| |
| --- ORMBadOrderEntryManager.php

然后,您只需将ORMBadOrderEntryManager注入BadOrderEntryList

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读