Utilisation de fetchObject()
Bonjour, Je crée un blog en php avec l'architecture MVC + entités.
On retrouve bien la vue, le model, le controller et l'index.
dans le model, j'ai 2 fichiers / 2 classes qui sont:
- PostManager.php
- CommentManager.php
Puis j'ai un dossier "Entity" dans lequel j'ai mes 2 classes Posts et Comment.
Voici l'entité et notamment ma classe Posts
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?php
class Posts {
private $id;
private $title;
private $content;
private $date;
/*ID*/
public function getId() {
return $this->id;
}
/*TITLE*/
public function getTitle() {
return $this->title;
}
public function setTitle($title) {
$this->title = $title;
}
/*CONTENT*/
public function getcontent() {
return $content->content;
}
public function setContent($content) {
$this->content = $content;
}
/*CREATION_DATE*/
public function getDate() {
return $date->creation_date;
}
public function setDate($date) {
$this->creation_date = $date;
}
} |
et la class PostManager :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
<?php
require_once('model/Manager.php');
class PostManager {
public function getPosts() {
$connexion = new Manager();
$db = $connexion->dbConnect();
$posts = $db->query('SELECT id, title, content, DATE_FORMAT(creation_date, \'%d/%m/%Y à %Hh%imin%ss\') AS creation_date_fr FROM posts ORDER BY creation_date DESC');
return $posts;
}
public function getPost($id) {
$connexion = new Manager();
$db = $connexion->dbConnect();
$req = $db->prepare('SELECT id, title, content, DATE_FORMAT(creation_date, \'%d/%m/%Y à %Hh%imin%ss\') AS creation_date_fr FROM posts WHERE id = ?');
$req->bindValue(1, $id, \PDO::PARAM_INT);
$req->execute(array($id));
$post = $req->fetchObject(__DIR__.'\Entity\Posts');
if($post) {
return $post;
}
//$req->execute(array($id));
//return $req; |
Comme vous le voyez dans la fonction getPost($id) je prépare une requête puis l'exécute en tentant de récupérer un objet (la classe Post qui est dans Entity) avec fetchObject()
Mais l'erreur me dit que la class dans /Entity/Posts est NOT FOUND. Je ne comprends pas, car mon fichier PostManager.php (là où j'écris ma fonction) est au même niveau que Entity.
En gros mes dossiers sont comme ça :
->Model
->->PostManager.php
->->Entity
->->->->Posts.php
->->->->Comment.php
->->model
->->->->Manager.php
Pourquoi je n'arrive pas à cibler le fichier voulu ?
Merci pour vos réponses