我目前正在努力从 Symfony 2.4.3 中的 nativquery
中获取结果。简单来说,我目前正在构建一个 JobQueue/MsgQueue 系统,它只会在队列中添加/删除作业。该过程将获取第一个作业,将其设置为事件状态并应该返回整个结果。正是问题所在 - 我无法获取任何内容。
我以此为例:How to execute Stored Procedures with Doctrine2 and MySQL
这是我在 ConsoleCommand 类
中使用的代码:
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"
}
最佳答案
解决方案
以下是我最终找到的解决方案。它不是最好的,因为不再有映射,但在我的情况下没有必要。
此外:我需要更改为获取 PDO_MySQL 的 getConnection 并进一步更改为 prepare
和 fetch()
函数。 vardump show 现在是合适的结果。
// 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"
}
关于php - Symfony2 + 存储过程 ||如何获取结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494062/