English 中文(简体)
我的事务所是否实施交易和承付方法?
原标题:Should my Service implements transaction & commit method?

我要说的是,我在<代码>上做了大量用户更新工作。 用户名册

www.un.org/Depts/DGACM/index_spanish.htm 我确实:

foreach ($users as $user) {
    $userService = new UserService();
    $user->updateUser($data);
}

如果用户很多,用户服务:更新 用户方法只是persist(/flush()

因此,我不禁要问,做这样的事情是否好:

class UserService {
  public function setUseTransaction($flag)
  {
      $this->useTransaction = $flag;
      return $this;
  }

  public function updateUser($data)
  {
     // some data mapping

     $entityManager->persist($user);

     if ($this->useTransaction) {
       $entityManager->flush();
     }
   }

   public function commit()
   {
      $entityManager->flush();
   }
}

然后在我的<代码>上填写。 我可以做到:

$userService = new UserService();
$userService->setUseTransaction(true);

foreach ($users as $user) {
    $userService = new UserService();
    $user->updateUser($data);
}

$userService->commit();

你们的想法是什么?

最佳回答

我不想暴露在我服务层之上的任何交易管理。 我可能把所有这些 st倒在我的服务中,暴露了两种更新User(用户意向)的公开方法(一对含蓄流),以及更新Users(用户信息)(关于最新数据)

Something roughly like:

class UserService {

    public function updateUser(User $user){
        $this->_updateUser();
        $this->em()->flush();        
    }

    public function updateUsers(array $users){
        foreach($users as $u) $this->_updateUser($u);
        $this->em()->flush();     
    }

    private function _updateUser(User $user){
        //do stuff to $user
        $this->em()->persist($user);
    }
}

然后,如果你后来决定你希望把最新消息nk到100个团体,或不管怎样,你的主编逻辑在服务中被完全考虑,而不是在你的控制者中可能有多个地方。

问题回答

在交易中将所有货物拆解无疑会加快。

编写全篇更新文件,作为单一文件库,将加快数百次。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签