2

我知道如何从数据库中获取所有结果我想要做的是获取选定的行。谁能给我一个示例代码。

public function getAllAddresses()
    {
        $dbTable = new Application_Model_DbTable_User();
        $result = $dbTable->fetchAll();

        $addresses = array();
        foreach ($result as $row) {

        $address = new Application_Model_User();
        $address->setuserId($row->userId)
            ->setuserName($row->userName)
            ->setaddressLine1($row->addressLine1)
            ->setaddressLine2($row->addressLine2)
            ->setaddressLine3($row->addressLine3)
            ->settelephone($row->telephone);

        $addresses[$row->userId] = $address;
    }

    return $addresses;
}

我该怎么做。。

这是我对 fetchAll 的控制器调用

public function indexAction()
    {
        // action body
        $addressMap = new Application_Model_UserMapper();
        $addresses = $addressMap->getAllAddresses();

        $this->view->addresses = $addresses;
    }

如何调用 fetch 方法?

这是我的看法

<?php if (isset($this->addresses)) : ?>
    <table>
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Street</th>
            <th>City</th>
            <th>Town</th>
            <th>Telephone</th>
        </tr>
        </thead>
        <tbody>
        <?php 
        foreach ($this->addresses as $userId => $address) {
            $trow = '<tr>';
            $s = $address->getuserId();
            $trow .= '<td>'. $address->getuserId() .'</td>';
            $trow .= '<td>'. $address->getuserName() .'</td>';
            $trow .= '<td>'. $address->getaddressLine1() .'</td>';
            $trow .= '<td>'. $address->getaddressLine2() .'</td>';
            $trow .= '<td>'. $address->getaddressLine3() .'</td>';
            $trow .= '<td>'. $address->gettelephone() .'</td>';
            $trow .= '<td><a href="Index?id='. $s .'">Edit</a></td>';
            $trow .= '</tr>';

            print $trow;
        }
        ?>
        </tbody>
    </table>
<?php else : ?>
    <p>Address not found</p>
<?php endif; ?>

我对 Zend 和 MVC 架构非常陌生。任何帮助将不胜感激。

4

2 回答 2

2

在这里,您可以在http://framework.zend.com/manual/1.12/en/zend.db.table.row.html获得完整的文档

你需要调用:fetchRow()方法而不是fetchAll()

于 2013-02-19T10:08:37.503 回答
0

假设您想id = 5从用户表中获取列名为“id”的行。$id = 5

$userObj = new Application_Model_DbTable_User();
$userRow = $userObj->fetchRow('id = '.$id);

现在您可以访问用户列

 echo $userRow->columnName;
于 2013-05-21T13:55:43.717 回答