2013-03-04 6 views
8

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; 
+0

tua domanda ha aiutato molto! grazie! –

risposta

2

Theres nessun supporto per la DOP direttamente per quanto ne sono a conoscenza. In genere, se è necessario creare un grafico di un oggetto complesso dal risultato di una query, è la responsabilità di un ORM.

Se hai bisogno di questa funzionalità, ti consiglio di utilizzare Doctrine o Propel anziché scrivere qualcosa tu stesso. Ce ne sono anche altri che potrebbero essere più leggeri, ma non ho esperienza con loro.

EDIT:

Penso che forse ho frainteso la questione im sicuro che altri potrebbero. Credo che la vera domanda è come ottenere l'accesso alle colonne unite, non cessarially come creare un oggetto da loro.

In tal caso, semplicemente utilizzando un metodo di fethc Arry standard come PDO::FETCH_ASSOC, PDO::FETCH_NUMERIC o PDO::FETCH_BOTH vi darà tutte le colonne interrogato.

Quindi, se si vuole trasformarla in un "oggetto grafico" si hanno a che fare non manualmente utilizzando PDO::FETCH_CLASS.

Ad esempio:

//$db is pdo: 
// also notice im aliase the columns prefixing the name so that we can tell what belongs to 
// post and what belongs to user, an alternative approach would be to use FETCH_NUMERIC, 
// which just uses the column positions from the seelct statement as the keys 
// so in this case post.id would be in the array as key 0, and user.name would be in the 
// array as key 4 
$stmt = $db->prepare('SELECT post.id as p_id, 
     post.text as p_text, 
     post.user_id as p_user_id, 
     user.id as u_id, 
     user.name as u_name 
FROM POST INNER JOIN User on post.user_id = user.id'); 

$stmt->execute(); 

while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { 
    print_r($row); 
    /* will output: 
     Array (
     'p_id' => 'value' 
     'p_text' => 'value' 
     'p_user_id' => 'value' 
     'u_id' => 'value', 
     'u_name' => 'value' 
    ) 
    So now you need to decide how to create your objects with the information returned 
    */ 
} 
+0

grazie.ATM Penso che avrei dovuto stare lontano da DOP se questo è troppo "complesso". Quali applicazioni esistono anche senza un join semplice? –

+0

Non capisco cosa sia così complicato a riguardo soprattutto considerando che l'unica altra opzione è 'mysqli', che è più difficile da lavorare e più prolissa. Non supportano "idratare" una relazione complessa da un risultato di una query unita. – prodigitalson

+0

Non sono sicuro di aver compreso correttamente il tuo commento. Sei d'accordo che DOP dovrebbe supportarlo? Lo supporta quando non usa FETCH_CLASS? Vedrò quello ora ... –

1
Non

davvero una risposta per il OQ, ma perché continua a comparire su Google (sì, lo so proprio più di un anno). Scoprirai che è incredibilmente più veloce saltare semplicemente i loop e interrogare ogni tabella separatamente.

 

    SELECT post.id, 
      post.text, 
      post.user_id, 
    FROM POST INNER JOIN User on post.user_id = user.id 
     $statement = $db -> prepare($query); 
     $statement -> execute(); 
     $statement -> setFetchMode(PDO::FETCH_CLASS, 'Post'); 
     $posts = $statement -> fetchAll(); 

    SELECT user.id, 
      user.name 
    FROM POST INNER JOIN User on post.user_id = user.id 
     $statement = $db -> prepare($query); 
     $statement -> execute(); 
     $statement -> setFetchMode(PDO::FETCH_CLASS, 'User'); 
     $users = $statement -> fetchAll(); 
0

Forse utilizzare PDO :: FETCH_NAMED se si lavora su più tabelle. Oppure usa PDO :: ATTR_FETCH_TABLE_NAMES.