Diciamo che ho 2 oggetti PHP:DOP FETCH_CLASS con tabelle unite
<?php
class Post {
public $id;
public $text;
public $user_id;
}
?>
e
<?php
class User {
public $id
public $name
}
?>
Ogni post non ha un vincolo univoco con 1 utente nel database.
Voglio inserire i dati nell'oggetto "Post" con i PDO "FETCH_CLASS" metodo che funziona per tutti gli attributi "Post" ma come faccio a riempire gli attributi in "Utente"?
My SQL-dichiarazione assomiglia a questo:
SELECT post.id,
post.text,
post.user_id,
user.id,
user.name
FROM POST INNER JOIN User on post.user_id = user.id
Grazie!
UPDATE:
ATM riempio la mia "Post" di classe in questo modo:
$statement = $db -> prepare($query);
$statement -> execute();
$statement -> setFetchMode(PDO::FETCH_CLASS, 'Post');
$posts = $statement -> fetchAll();
Così come avrei dovuto cambiare la situazione per riempire anche l'altra classe "Utente"?
SOLUZIONE:
$statement = $db -> prepare($query);
$statement -> execute();
$posts = array();
while (($row = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
$post = new Post();
$post->id = $row['post_id'];
$post->text = $row['post_text'];
$post->created = $row['post_created'];
$post->image = $row['post_image'];
$post->url = $row['post_url'];
$post->weight = $row['post_weight'];
$post->likes = $row['post_likes'];
$user = new User();
$user->id = $row['user_id'];
$user->nickname = $row['user_nickname'];
$user->created= $row['user_created'];
$user->locked = $row['user_locked'];
$post->user = $user;
$posts[] = $post;
}
return $posts;
tua domanda ha aiutato molto! grazie! –