我目前正在努力从nativquery
Symfony 2.4.3中获取结果。为简单起见,我目前正在构建一个JobQueue / MsgQueue系统,它将仅在队列中添加/删除作业。该过程将获取第一个作业,将其设置为活动状态,并应返回整个结果。确实存在问题-我什么都拿不到。
我以以下示例为例:如何使用Doctrine2和MySQL执行存储过程
这是我在中使用的代码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"
}
解决方案
以下是我终于找到的解决方案。它不是最好的,因为不再有Mapping了,但就我而言,这是没有必要的。
进一步:我需要更改为getConnection来获取PDO_MySQL并进一步更改为prepare
和fetch()
函数。现在,vardump节目是合适的结果。
// 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"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句