gpt4 book ai didi

php - ZF2、Oracle、SlmQueueDoctrine、ClearObjectManagerStrategy 不工作

转载 作者:可可西里 更新时间:2023-11-01 12:57:34 26 4
gpt4 key购买 nike

我有一个具有以下配置的 ZF2 项目。它使用 Doctrine ORM 和 SlmQueue。由于 SlmQueue 不支持我们的命名约定和 oracle 数据库,我们定制了 SlmQueueDoctrine。

我怀疑我的工作没有使用 ClearObjectManagerStrategy 并且在执行单个作业之前没有清除 ObjectManager。

它不反射(reflect)队列启动后的数据库修改。但如果我终止队列守护进程并重新启动,它会选择新的值。

如何在执行单个作业之前实现 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' => 'SlmQueueDoctrine\Factory\DoctrineQueueFactory'
),
),
'job_manager' => array(
'factories' => array(
'Report\Job\Rank' => 'Report\Job\RankFactory',
),
'shared' => array(
'Report\Job\Rank' => false
),
),
'queues' => array(
'doctrineQueue' => array(
'table_name' => 'IOQUEUE'
)
)
)
);
?>

module/Report/src/Report/Job/Rank.php

<?php
namespace Report\Job;

use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use DoctrineModule\Persistence\ProvidesObjectManager as ProvidesObjectManagerTrait;
use SlmQueue\Job\AbstractJob;

use Application\Entity\Report;

use Application\Log\LoggerAwareInterface;
use Application\Log\LoggerAwareTrait;

use Application\Service\ReportService;

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 , 但预期的行为。

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

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

your comment 你说“它不会刷新对象管理器的和当前事务”。这不是您可以通过调用 clear 期望的操作。根据文档,Detach 会导致以下操作:

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.

关于php - ZF2、Oracle、SlmQueueDoctrine、ClearObjectManagerStrategy 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com