PHP - 将准备好的stmt提取到类中:致命错误"找不到类"

kin*_*ske 2 php mysql pdo prepared-statement

我想将查询结果提取到一个类(到一个类的实例数组).但是我收到以下错误消息:致命错误:找不到类'类别'...这是我的数据库管理器类中涉及的两个函数的代码:

public function prepareStatement($_Statement)
{
    $this->preparedStmt = $this->pdo->prepare($_Statement);

    if($this->preparedStmt === FALSE)
        throw new PreparedStmtException ("Fehler: Statement konnte nicht prepared werden.");
    else
        return TRUE;
}


public function execute($_Params = array(), $_FetchMode = NULL, $_Class = NULL)
{
    # Cancel execution if no statement prepared
    if($this->preparedStmt === null) 
        throw new PreparedStmtException ("Fehler: Statement wurde vor execute nicht prepared.");

    try
    {
        # Execute PDO call with params
        $this->preparedStmt->execute($_Params);

        # If no data is returned throw NoDataException
        if($this->preparedStmt->columnCount() == 0)
            throw new NoDataException;

        // else
        // Determine which fetch mode should be called, if NULL or something != 1 || != 0 just call
        // fetchAll without params
        if ($_FetchMode == 1)
             $result = $this->preparedStmt->fetchAll(PDO::FETCH_ASSOC); 

        else if ($_FetchMode == 2)
             $result = $this->preparedStmt->fetchAll(PDO::FETCH_CLASS, $_Class); 

        else 
            $result = $this->preparedStmt->fetchAll();
    }
    catch (PDOException $e)
    {
        # Errormanagement --> Message im live Betrieb rausnehmen
        echo '<div style="color: red;">'.$e->getMessage().'</div>';
        $result = FALSE;
    }

    // If result is null throw Instance Exception, if result emtpy throw NoDataException
    if ($result == null)
        throw new InstanceException;
    else if (empty($result))
        throw new NoDataException;

    return $result;
}
Run Code Online (Sandbox Code Playgroud)

这是一个类中的测试函数来调用它们:

public function test () 
{
    $stmt = "SELECT * FROM tx_exhibition_category WHERE uid = 1 OR uid = 2";
    $this->mysql->prepareStatement($stmt);
    return $this->mysql->execute (array(), 2, "Category");
}
Run Code Online (Sandbox Code Playgroud)

这就是我称之为测试功能的方式:

$comment = CommentQuery::getInstance();
$result = $comment->test();
var_dump($result); // should be an array with instances of Category
Run Code Online (Sandbox Code Playgroud)

这是应该被引入的类:

class Category {

private $id;
private $name;
private $projectId;

// getter and setter...
}
Run Code Online (Sandbox Code Playgroud)

一些额外的信息:

  • 我使用自动加载器来包含我的类.
  • 我使用命名空间
  • 是的,可以在所有三个函数中创建类的实例,因此
    将包含类并使用名称空间
  • $ _Mode == 1工作正常

有任何想法吗?

chr*_*guy 7

如果您的Category类位于命名空间中,则需要将完全限定的类名传入fetchAll.

现在,PDO正在尝试获取Category根命名空间中的类.它不存在.您需要告诉PDO有关命名空间的信息:

$stm->fetchAll(\PDO::FETCH_CLASS, 'Vendor\\Package\\Category');
Run Code Online (Sandbox Code Playgroud)

或者使用__NAMESPACE__常量,如果它更容易(并且是正确的):

$stm->fetchAll(\PDO::FETCH_CLASS, __NAMESPACE__ . '\\Category');
Run Code Online (Sandbox Code Playgroud)

或者,更好的是,使用PHP 5.5 +的::class常量来表示完全限定的类名.

use Acme\Package\Category;
$stm->fetchAll(\PDO::FETCH_CLASS, Category::class);
Run Code Online (Sandbox Code Playgroud)