I want to fetch an object from a MySQL database, which has a table called 'Article'. The getLatestArticle() function is as following:
public function getLatestArticle($priority){
$query = "select * from article where priority = :priority having date = max(date);";
$STH = $this->dbLink->prepare($query);
$STH->bindParam(':priority', $priority);
if(!$STH->execute()){
print_r($STH->errorInfo());
}else{
return $STH->fetchObject('Article');
}
}
Having included the classes properly, and having the names of class variables, constructor parameters and column names in mysql identical produces this:
object(Article)#6 (8) { ["id":"Article":private]=> string(2) "13" ["title":"Article":private]=> string(0) "" ["subTitle":"Article":private]=> string(0) "" ["imgFilePath":"Article":private]=> string(0) "" ["contentFilePath":"Article":private]=> string(0) "" ["author":"Article":private]=> string(0) "" ["date":"Article":private]=> string(0) "" ["priority":"Article":private]=> string(0) "" }
I get an object of the class Article as expected, although only the field 'id' is correct while all the others are using default values. I tried using setFetchMode(PDO::FETCH_OBJ, 'Article') and using fetch(), also tried using setFetchMode(PDO::FETCH_CLASS, 'Article) and using fetch() without any luck. All the class variables in Article are specified as private and the constructor is presented like so:
public function __construct($title="", $subTitle="", $imgFilePath="", $contentFilePath="", $author="", $date="", $priority = ""){
$this->title = $title;
$this->subTitle = $subTitle;
$this->imgFilePath = $imgFilePath;
$this->contentFilePath = $contentFilePath;
$this->author = $author;
$this->date = $date;
$this->priority = $priority;
}
I tried removing the default values in the constructor and changing the class members from private to public, produces a missing argument error.
Why do objects retrieved through fetchObject('Article') end up with only the id field with a correct value while all other fields use the default value?
EDIT: Forgot to mention, fetching an associative array will have all the fields filled correctly, but i would like to do it with fetchObject()