問題描述
我想將查詢的結果提取到一個類中(到一個類的實例數組中).但我收到以下錯誤消息:致命錯誤:在...中找不到類類別"這是我的數據庫管理器類中涉及的兩個函數的代碼:
I want to fetch the result of a query into a class (into an array of instances of a class). But I get the following error message: Fatal error: Class 'Category' not found in ... This is the code of the two functions in my database manager class that are involved:
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;
}
這是一個類中的測試函數來調用它們:
This is a test function in a class to call them:
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");
}
這就是我調用測試函數的方式:
This is how i call test function:
$comment = CommentQuery::getInstance();
$result = $comment->test();
var_dump($result); // should be an array with instances of Category
這是它應該被提取到的類:
And this is the class where it should be fetched into:
class Category {
private $id;
private $name;
private $projectId;
// getter and setter...
}
一些附加信息:
- 我使用自動加載器來包含我的類.
- 我使用命名空間
- 是的,可以在所有三個函數中創建類的實例,所以
將包含類并使用命名空間 - $_Mode == 1 工作正常
有什么想法嗎?
推薦答案
如果您的 Category
類在命名空間中,您需要將完全限定的類名傳入 fetchAll
.
If your Category
class is in a namespace, you'll need to pass in a fully qualified class name into fetchAll
.
現在,PDO 正在嘗試獲取根命名空間中的 Category
類.它不存在.你需要告訴 PDO 關于命名空間:
Right now, PDO is trying to fetch into the class Category
in the root namespace. It doesn't exist. You need to tell PDO about the namespace:
$stm->fetchAll(PDO::FETCH_CLASS, 'Vendor\Package\Category');
或者使用 __NAMESPACE__
常量,如果這樣更容易(并且正確):
Or use a __NAMESPACE__
constant if that makes it easier (and is correct):
$stm->fetchAll(PDO::FETCH_CLASS, __NAMESPACE__ . '\Category');
或者,更好的是,使用 PHP 5.5+ 的 ::class
常量來獲取完全限定的類名.
Or, even better, use PHP 5.5+'s ::class
constant to ge the fully qualified class name.
use AcmePackageCategory;
$stm->fetchAll(PDO::FETCH_CLASS, Category::class);
這篇關于PHP - 將準備好的 stmt 提取到類中:致命錯誤“找不到類"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!