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

php – ZF2,Oracle,SlmQueueDoctrine,ClearObjectManagerStrateg

发布时间:2020-12-13 18:03:21 所属栏目:PHP教程 来源:网络整理
导读:我有一个ZF2项目,具有以下配置.它使用的是Doctrine ORM和SlmQueue.由于SlmQueue不支持我们的命名约定和oracle数据库,因此我们定制了SlmQueueDoctrine. 我怀疑我的工作不是使用ClearObjectManagerStrategy,并且在执行单个作业之前不会清除ObjectManager. 它在
我有一个ZF2项目,具有以下配置.它使用的是Doctrine ORM和SlmQueue.由于SlmQueue不支持我们的命名约定和oracle数据库,因此我们定制了SlmQueueDoctrine.

我怀疑我的工作不是使用ClearObjectManagerStrategy,并且在执行单个作业之前不会清除ObjectManager.

它在队列启动后不反映数据库修改.但是如果我杀死队列守护进程并重新开始,它会选择新的值.

如何在执行单个作业之前实现ClearObjectManagerStrategy并清除ObjectManager?

我试过很多没有运气的人.

composer.json

{
    "repositories": [
        {
            "url": "https://github.com/pradeep-sanjaya/doctrine-extensions.git","type": "git"
        }
    ],"require": {
        "php": ">=5.3.3","zendframework/zendframework": "2.3.3","doctrine/doctrine-orm-module": "0.7.*","pradeep-sanjaya/doctrine-extensions": "dev-master","spoonx/sxmail": "1.4.*","slm/locale": "dev-master","imagine/Imagine": "0.6.*","tecnick.com/tcpdf": "dev-master","slm/queue": "0.4.*","slm/queue-doctrine": "0.4.*"
    }
}

配置/自动加载/ slm_queue.local.php

<?php
return array(
    'slm_queue' => array(
        'queue_manager' => array(
            'factories' => array(
                'doctrineQueue' => 'SlmQueueDoctrineFactoryDoctrineQueueFactory'
            ),),'job_manager' => array(
            'factories' => array(
                'ReportJobRank' => 'ReportJobRankFactory','shared' => array(
                'ReportJobRank' => false
            ),'queues' => array(
            'doctrineQueue' => array(
                'table_name' => 'IOQUEUE'
            )
        )
    )
);
?>

模块/报告/ src目录/报告/招聘/ Rank.php

<?php
namespace ReportJob;

use DoctrineModulePersistenceObjectManagerAwareInterface;
use DoctrineModulePersistenceProvidesObjectManager as ProvidesObjectManagerTrait;
use SlmQueueJobAbstractJob;

use ApplicationEntityReport;

use ApplicationLogLoggerAwareInterface;
use ApplicationLogLoggerAwareTrait;

use ApplicationServiceReportService;

class Rank extends AbstractJob implements ObjectManagerAwareInterface,LoggerAwareInterface
{
    use LoggerAwareTrait;
    use ProvidesObjectManagerTrait;

    /**
     * @var ReportService
     */
    protected $reportService;

    /**
     * @var array
     */
    protected $reportId = array();

    public function setReportService(ReportService $reportService)
    {
        $this->reportService = $reportService;
    }

    /**
     * Execute the job
     *
     * @return void
     */
    public function execute()
    {
        //clear object manager does not work
        //$om = $this->getObjectManager();
        //$om->clear();

        $content = $this->getContent();
        $this->setReportId($content['reportId']);

        if (!empty($this->reportId)) {
            try {
                if (is_array($this->reportId)) {
                    foreach ($this->reportId as $reportId) {
                        $this->updateRank($reportId);
                    }
                    unset($reportId);
                } else {
                    $this->updateRank($this->reportId);
                }
            } catch (Exception $exception) {
                echo "Exception message is {$exception->getMessage()} n";
            }
        }
    }

    private function updateRank($reportId)
    {
        /* @var $report Report */
        $report = $this->reportService->getReport($reportId);
        $this->logInfo(print_r($report,true)); // this always return older db values,the values before it start queue deamon

        if (!$report instanceof Report) {
            return;
        }

        if (empty($rankData)) {
            return;
        }

        //more codes,application related logics

        $this->reportService->updateReportEntity($report);
    }

    private function setReportId($reportId)
    {
        if (is_numeric($reportId)) {
            $this->reportId = array($reportId);
        } elseif (is_array($reportId)) {
            $this->reportId = $reportId;
        } else {
            throw new Exception('Expects reportId as int or array');
        }
    }
}
如果你的意思是它没有完全清楚那么显然是 this is not a bug,但预期的行为.

您可以在调用clear方法时检查the documentation chapter 7.5.的行为:

When EntityManager#clear() is invoked,all entities that are currently managed by the EntityManager instance become detached.

在your comment中,您说“它不会刷新对象管理器和当前事务”.这不是您可以通过调用清除所期望的操作.根据文档,分离会导致以下操作:

The semantics of the detach operation,applied to an entity X are as follows:

  • If X is a managed entity,the detach operation causes it to become detached. The detach operation is cascaded to entities referenced by X,if the relationships from X to these other entities is mapped with cascade=DETACH or cascade=ALL (see “Transitive Persistence”). Entities which previously referenced X will continue to reference X.
  • If X is a new or detached entity,it is ignored by the detach operation.
  • If X is a removed entity,the detach operation is cascaded to entities referenced by X,if the relationships from X to these other entities is mapped with cascade=DETACH or cascade=ALL (see “Transitive Persistence”). Entities which previously referenced X will continue to reference X.

(编辑:李大同)

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

    推荐文章
      热点阅读