php - Symfony2 + 存储过程 ||如何获取结果?

标签 php mysql symfony stored-procedures symfony-2.4

我目前正在努力从 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 并进一步更改为 preparefetch() 函数。 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/

相关文章:

php - Codeigniter 中丢失 session 数据

php - 如何创建限制应用程序中用户存储的解决方案?

php - 在mysql中的序列化php中存储多语言数据

mysql - 复制 "on commit"

php - Symfony2 : How to properly include assets in conjunction with Twig template inheritance?

php - 允许添加的 Symfony 表单实体

php - 在不使用 ON CASCADE 的情况下在 PHP 代码的各行中准备语句

apache - 在 Ubuntu (Apache2/php5-fpm/Mysql) 上安装 Phabricator 时出现错误 500

java - 在 JPA 中插入多行的最有效方法

php - 奏鸣曲日期范围过滤器