在Zend Framework中获取选定的行

时间:2013-02-19 09:41:53

标签: php database zend-framework fetch

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

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架构的新手。 任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:2)

这里有完整的文档 http://framework.zend.com/manual/1.12/en/zend.db.table.row.html

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

答案 1 :(得分:0)

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

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

现在您可以访问用户列

 echo $userRow->columnName;