2013-03-04 63 views
8

比方說,我有2個PHP對象:與連接表PDO FETCH_CLASS

<?php 
class Post { 
    public $id; 
    public $text; 
    public $user_id; 
} 
?> 

<?php 
class User { 
    public $id 
    public $name 
} 
?> 

每一個崗位有1個用戶在數據庫中的唯一約束。

我想用PDO「FETCH_CLASS」方法將數據填充到「Post」對象中,該方法適用於所有「Post」屬性,但是如何填充「User」中的屬性?

我的SQL語句是這樣的:

SELECT post.id, 
     post.text, 
     post.user_id, 
     user.id, 
     user.name 
FROM POST INNER JOIN User on post.user_id = user.id 

謝謝!

UPDATE:

ATM我補我的「郵報」級這樣的:

$statement = $db -> prepare($query); 
    $statement -> execute(); 
    $statement -> setFetchMode(PDO::FETCH_CLASS, 'Post'); 
    $posts = $statement -> fetchAll(); 

那麼,如何將不得不改變,對於又充盈着其他類「用戶」?

SOLUTION:

$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

你的問題幫了很多!謝謝! – 2017-01-28 09:38:09

回答

2

即使世界就我所知在PDO直接的支持。通常,如果您需要根據查詢的結果創建複雜的對象圖表,那麼ORM的職責就是這樣。

如果您需要此功能,我建議您使用DoctrinePropel而不是自己寫點東西。還有其他人可能更輕,但我沒有與他們的經驗。

編輯:

我想,也許我誤解了這個問題作爲即時通訊相信其他人可能。我認爲真正的問題是如何訪問連接的列,而不是如何從它們創建對象。

在這種情況下,只需使用標準arry fethc方法(如PDO::FETCH_ASSOC,PDO::FETCH_NUMERICPDO::FETCH_BOTH)即可爲您提供查詢的所有列。

所以,如果你想把它變成一個「對象圖」,你必須手動執行而不是使用PDO::FETCH_CLASS

例如:

//$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

謝謝。ATM我認爲如果這太「複雜」,我應該遠離PDO。沒有簡單的連接,哪些應用程序甚至存在? – 2013-03-04 13:44:36

+0

我不明白什麼是如此複雜,特別是考慮到唯一的其他選項是'mysqli',它更難以使用和更詳細。既不支持從聯合查詢結果中「補充」複雜的關係。 – prodigitalson 2013-03-04 13:50:19

+0

我不確定是否正確理解您的評論。你是否同意PDO應該支持?當不使用FETCH_CLASS時它是否支持它?我會考慮,現在... – 2013-03-04 13:54:40

1

沒有真正的OQ的響應,而是因爲它不斷出現在谷歌(是的,我一歲的知道它的結束)。您會發現,僅僅跳過循環並單獨查詢每個表的速度會快得驚人。

 

    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

也許使用PDO :: FETCH_NAMED如果你工作的多個表。或者使用PDO :: ATTR_FETCH_TABLE_NAMES。