Bonjour,
Voila je rencontre un petit problème avec mon code. Je suis la vidéo Refactoring de la formation POO et je ne comprends pas pourquoi ma fonction find() retourne un tableau avec un objet au lieu d'un objet.
J'ai fais un var_dump de $var = $app->getTable('Article')->find($_GET['id']);, j'obtiens ça
array (size=1)
0 =>
object(App\Entite\ArticleEntite)[7]
public 'id' => string '2' (length=1)
public 'date' => string '2016-06-01' (length=10)
public 'titre' => string 'lol' (length=3)
public 'contenu' => string 'Bender, you risked your life to save me! Bender, you risked your life to save me! You lived before you met me?! Yes! In your face, Gandhi! Look, last night was a mistake. Then throw her in the laundry room, which will hereafter be referred to as "the brig".
Say it in Russian! No! The cat shelter's on to me. Uh, is the puppy mechanical in any way? How much did you make me?
Leela's gonna kill me. You're going back for the Countess, aren't you? What's with you kids? Every other day it's food, food, food.'... (length=1149)
public 'online' => string '0' (length=1)
public 'type' => string '' (length=0)
Voici find(), prepare() et les deux query()
Dans Database.php
public function query($statement, $class_name, $one=false){
$req = $this->GetPDO()->query($statement);
$req->setFetchMode(PDO::FETCH_CLASS, $class_name);
if($one){
$datas = $req->fetch();
}
else{
$datas = $req->fetchAll();
}
return $datas;
}
public function prepare($statement, $attributes, $class_name, $one = false)
{
$req = $this->GetPDO()->prepare($statement);
$req->execute($attributes);
$req->setFetchMode(PDO::FETCH_CLASS, $class_name);
if($one){
$datas = $req->fetch();
}
else{
$datas = $req->fetchAll();
}
return $datas;
}
Dans Table.php
public function query($statement, $attributes = null, $one = false){
if($attributes)
{
return $this->db->prepare(
$statement,
$attributes,
str_replace('Table', 'Entite', get_class($this),
$one));
}
else{
return $this->db->query(
$statement,
str_replace('Table', 'Entite', get_class($this),
$one));
}
}
public function find($id){
return $this->query("SELECT * FROM {$this->table} WHERE id = ?",[$id], true);
}
Actuellement pour afficher tout comme Grafikart, j'ai fais ça
$post = $var[0];
<h1><?= $post->titre; ?></h1>
Mais j'aimerais comprendre d'où viens le problème :/ Merci