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...
}
Some additional info:
- I use an autoloader to include my classes.
- I use namespaces
- yes, it is possible to create an instance of the class in all of the three functions, so
class would be included and namespace used
- $_Mode == 1 works fine
Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…