2

我有2多對多關係表;帖子和分類。一篇文章可以有很多類別。我的問題是如何顯示其分類的帖子列表?Codeigniter Datamapper多對多列表

就像是:

我交1(CAT1,CAT2,CAT3)
我交2(CAT2,CAT3)
我交3(CAT1)

我已經試過這些方法;

// Create post object 
$p = new Post(); 

// Get 30 posts 
$p->get(30); 

// Loop through all posts 
foreach ($p as $post) 
{ 
    // Get the current user's group 
    $post->category->get(); 

    foreach($post->category as $category) { 
     // ... 
    } 
} 

不喜歡這一點,因爲如果我得到30個職位,然後在每一個崗位循環再次進行查詢,並一次又一次地找到類別。

,並試圖此:

$p = new Post(); 

$p->include_related('category', array('id', 'name'), TRUE, TRUE)->get(30); 

foreach($p as $post) { 
    // ... 
    foreach($post->category as $category) { 
    // ... 
    } 
} 

這是更接近,但這個問題是我設置的限制get(30)因此,如果平均每個職位有我的2個大類顯示15個後+ 15個類別。

多對多上市的真實方法是什麼?

回答

0

那麼在這種情況下,我會選擇在php關聯數組中緩存兩個表,然後循環數組。