Kevin de Vos Kevin de Vos - 4 months ago 14
MySQL Question

PHP PDO fetchObject() - object containing only default values except id member

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.


  1. Can anyone explain why the missing argument error occurs even with class variable members on public?

  2. 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()

Answer

I will skip question 1, because you start with question 2, and one question is enough. You don't show all the code involved, so I have to make guesses.

I have noted in the PHP manual that it says: "When an object is fetched, its properties are assigned from respective column values, and afterwards its constructor is invoked.". So you when your constructor is:

class Article
{
  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;
  }
}

The constructor will overwrite the column values retrieved with PDOStatement::fetchObject.

To do it correctly use this:

class Article
{
  private $title = '';
  private $subTitle = '';
  private $imgFilePath = '';
  private $contentFilePath = '';
  private $author = '';
  private $date = '';
  private $priority = '';

  public function __construct() {
    // do whatever you need, but do not overwrite the column values
  }
}

I hope this helps. If you have to set 'column values' you can use setters in the class.