我目前正在努力从nativquery
Symfony 2.4.3 中获取结果。简单来说,我目前正在构建一个 JobQueue/MsgQueue 系统,它只会在队列中添加/删除作业。该过程将获取第一个作业,将其设置为活动状态并应返回整个结果。正是问题所在——我什么也拿不到。
我以此为例:How to execute Stored Procedures with Doctrine2 and MySQL
这是我在 a 中使用的代码ConsoleCommand Class
:
protected function execute(InputInterface $input, OutputInterface $output)
{
## start
$output->writeln('<comment>Starting JobQueue Ping process</comment>');
// set doctrine
$em = $this->getContainer()->get('doctrine')->getManager();
$rsm = new ResultSetMapping;
$result = $em->createNativeQuery(
'CALL JobQueueGetJob (' .
':jobTypeCode' .
')', $rsm
);
$result->setParameters(array('jobTypeCode' => 1));
$result->execute();
$em->flush();
if ($input->getOption('verbose')) {
$output->writeln(var_dump($result->getResult()));
}
}
在这里,您可以使用过程代码和结果:
代码
PROCEDURE `JobQueueGetJob`(IN `jobType` TINYINT(2))
BEGIN
DECLARE jId int(11);
SELECT `msgId` into jId FROM `jobqueue` WHERE `MsgTypeCode` = jobType AND `jState` = 'N' LIMIT 1;
IF jId IS NOT NULL THEN
UPDATE `jobqueue` SET `jState` = 'A' WHERE `msgId` = jId;
SELECT * FROM `jobqueue` WHERE `msgId` = jId;
END IF;
END
结果通过 phpMyAdmin
Your SQL query has been executed successfully
0 rows affected by the last statement inside the procedure
SET @p0 = '1';
CALL `JobQueueGetJob` (
@p0
);
正如文本所暗示的,它不会返回任何结果,而是过程中的最后一条语句应该是查询本身。
解决方案(不是最好的)
命令:
// set doctrine
$em = $this->getContainer()->get('doctrine')->getManager()->getConnection();
// prepare statement
$sth = $em->prepare("CALL JobQueueGetJob(1)");
// execute and fetch
$sth->execute();
$result = $sth->fetch();
// DEBUG
if ($input->getOption('verbose')) {
$output->writeln(var_dump($result));
}
输出:
array(5) {
'msgId' =>
string(3) "122"
'msgTypeCode' =>
string(1) "1"
'jobCode' =>
string(22) "http://mail.google.com"
'jstate' =>
string(1) "A"
'created_at' =>
string(19) "2014-02-01 03:58:42"
}