2011-12-07 72 views
2

我希望能夠通過一個查詢來選擇多行。我想知道你是否可以做這樣的事情?活動記錄:使用db對象

$teamsQuery = $this->db; 

foreach($teamids as $teamid) 
$teamsQuery->where('teamid', $teamid); 

$teamsQuery->get('team'); 

$teams = $teamsQuery->result(); 

print_r($teams); 

回答

1

根據定義,ActiveRecord包裝在一個數據庫中的單個行中的對象,重視業務和持久邏輯吧。如果您想用一個查詢獲取多行,請查看TableDataGateway

在TableDateGateway中,您將有一個方法findTeamsByIds,您將傳遞整個ID數組,然後在SELECT … WHERE … IN查詢中獲取這些數據。

0

你可以嘗試這種類型的函數

/** 
* get all data from the table 
* 
* @param string $sql - mySQL query string 
* 
* @return data as associative array 
* */ 
function getAll($sql) { 
    $res = mysql_query($sql); 

    if (!$res) { 
     die(mysql_error()); 
    } 

    // Convert to array 
    $ret = array(); 
    while ($row = mysql_fetch_assoc($res)) { 
     // Add to array 
     $index = count($ret); 
     $ret[$index] = $row; 
    } 

    return $ret; 
} 
+0

什麼是while循環?它將該行作爲關聯數組提取,然後將該行分配給$ ret [$ index],但由於$ index始終是相同的數字,$ ret將僅包含最後返回的行。 – Gordon