简体   繁体   English

如何捕获fetchAll(PDO :: FETCH_OBJ)

[英]How to catch fetchAll(PDO::FETCH_OBJ)

In my Symfony 1.4 project in one of my class.php file I used the following codes to execute query and fetch object. 在我的class.php文件之一的Symfony 1.4项目中,我使用以下代码执行查询和获取对象。

$statement = Doctrine_Manager::connection()->execute($query);
$resultset = $statement->fetchAll(PDO::FETCH_OBJ);

I tried to use try-catch block to catch any run-time exceptions. 我试图使用try-catch块来捕获任何运行时异常。 I can easily do that for the first line like 我可以像第一行一样轻松地做到这一点

    try {
        $statement = Doctrine_Manager::connection()->execute($query);
    } 
    catch (Exception $exc) {
        echo "custom error message";
    }

But, I this dosen't work for the second line. 但是,我对第二行不起作用。 It always show the following error when I run the page 当我运行页面时,它总是显示以下错误

Fatal error: Call to a member function fetchAll() on a non-object in 致命错误:在非对象中调用成员函数fetchAll()

\\lib\\model\\doctrine\\DeductionInfo.class.php on line 104 第104行的\\ lib \\ model \\ doctrine \\ DeductionInfo.class.php

How can I resolve this? 我该如何解决?

Of course it seems to be an invalid query or something like that, resulting in a non-object in your var $statement. 当然,这似乎是无效的查询或类似的查询,导致var $ statement中出现非对象。 Debug or var_dump your $statement var at first. 首先调试或var_dump您的$ statement var。

Doctrine btw is on top of PDO. 学说是在PDO之上。 By default a PDO construct throws an exception on error. 默认情况下,PDO构造会在错误时引发异常。 But you have to set error handling for quering to exception ( http://php.net/manual/en/pdo.error-handling.php ) if you want to use try-catch statements. 但是,如果要使用try-catch语句,则必须设置用于查询异常的错误处理( http://php.net/manual/en/pdo.error-handling.php )。 I don't know exactly the way Doctrine 1 handles this. 我不知道教义1的处理方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM