活动记录:处理db对象


Active records: Working with a db object

我希望能够选择多行与一个查询。我想知道你能不能做这样的事?

$teamsQuery = $this->db;
foreach( $teamids as $teamid )
$teamsQuery->where('teamid', $teamid);
$teamsQuery->get('team');
$teams = $teamsQuery->result();
print_r($teams);

根据定义,ActiveRecord将数据库中的单行包装到对象中,并将业务和持久性逻辑附加到其上。如果你想在一次查询中获取多行,可以看看TableDataGateway。

在TableDateGateway中,您将有一个方法findTeamsByIds,您将传递整个id数组,然后在SELECT … WHERE … IN查询中获取这些。

您可以尝试这种类型的函数

 /**
 * 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;
}