Php 活动记录:使用db对象

Php 活动记录:使用db对象,php,database,object,Php,Database,Object,我希望能够通过一个查询选择多行。我想知道你能不能做这样的事 $teamsQuery = $this->db; foreach( $teamids as $teamid ) $teamsQuery->where('teamid', $teamid); $teamsQuery->get('team'); $teams = $teamsQuery->result(); print_r($teams); 根据定义,将数据库中的一行包装到对象中,并将业务和持久性逻辑附加到

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

$teamsQuery = $this->db;

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

$teamsQuery->get('team');

$teams = $teamsQuery->result();

print_r($teams);
根据定义,将数据库中的一行包装到对象中,并将业务和持久性逻辑附加到该对象中。如果希望通过一个查询获取多行,请查看


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

您可以尝试此类型的函数

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

那个while循环是干什么的?它以关联数组的形式获取该行,然后将该行分配给$ret[$index],但由于$index始终是相同的数字,$ret将只包含最后返回的行。