I'm currently working with one of my friends on making a portfolio for all of his projects and as strangely as it seems, I cannot manage to make setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Class Namespace') working on his code while it is working on my projects.
Here is the error returned by PHP : !(https://cdn.discordapp.com/attachments/525023910698156034/583938068311179265/unknown.png)
Here is the class Entity which contains the __construct() function and the hydration() function :
<?php
namespace App\Entity;
class Entity {
public function __construct(array $array) {
$this->hydrate($array);
}
public function hydrate($array) {
foreach ($array as $key => $value) {
$setter = 'set' . ucfirst($key);
if (method_exists($this, $setter)) {
$this->$setter($value);
}
}
}
}
Then, here is the Project Class which implements the class Entity :
<?php
namespace App\Entity;
class Project extends Entity implements \JsonSerializable {
private $_title;
private $_description;
private $_imagePath;
private $_link;
private $_repoLink;
private $_creationDate;
public function jsonSerialize() {
return [
'title' => $this->_title,
'description' => $this->_description,
'imagePath' => $this->_imagePath,
'link' => $this->_link,
'repoLink' => $this->_repoLink,
'creationDate' => $this->_creationDate,
];
}
/
* @return string
*/
public function getTitle(): string {
return $this->_title;
}
/
* @param string $title
*/
public function setTitle(string $title) {
$this->_title = $title;
}
/
* @return string
*/
public function getDescription(): string {
return $this->_description;
}
/
* @param string $description
*/
public function setDescription(string $description) {
$this->_description = $description;
}
/
* @return string
*/
public function getImagePath(): string {
return $this->_imagePath;
}
/
* @param string $imagePath
*/
public function setImagePath(string $imagePath) {
$this->_imagePath = $imagePath;
}
/
* @return string
*/
public function getLink(): string {
return $this->_link;
}
/
* @param string $link
*/
public function setLink(string $link) {
$this->_link = $link;
}
/
* @return string
*/
public function getRepoLink(): string {
return $this->_repoLink;
}
/
* @param string $repoLink
*/
public function setRepoLink(string $repoLink) {
$this->_repoLink = $repoLink;
}
/
* @return \DateTime
*/
public function getCreationDate(): \DateTime {
return $this->_creationDate;
}
/
* @param string $creationDate
*/
public function setCreationDate(string $creationDate) {
$this->_creationDate = new \DateTime($creationDate);
}
}
And finally, here is the SQL request :
<?php
namespace App\Model;
class ProjectManager extends Manager {
/**
* return a collection of Project objects
* @return Project[]
* @throws \Exception
*/
public function getProjects() {
$db = $this->getDb();
$q = $db->query(
'SELECT id,
title,
description,
image_path AS imagePath,
link,
repo_link AS repoLink,
creation_date AS creationDate
FROM my_website_projects
ORDER BY creationDate'
);
$q->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, 'App\Entity\Project');
$projects = $q->fetchAll();
return $projects;
}
}
The only thing that seems to work is to add PDO::FETCH_ASSOC in the fetchAll() but then it doesn't return an object but an array....
Your help would be much appreciated on this problem ! :)