我知道如何从数据库中获取所有结果我想做的是提取一个选定的行。有人能给我一个示例代码吗。
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架构非常陌生。任何帮助都将不胜感激。
这里有完整的文档http://framework.zend.com/manual/1.12/en/zend.db.table.row.html
您需要调用:fetchRow()
方法而不是fetchAll()
假设您想从用户表中获取列名为"id"和id = 5
的行。$id = 5
$userObj = new Application_Model_DbTable_User();
$userRow = $userObj->fetchRow('id = '.$id);
现在您可以作为访问用户列
echo $userRow->columnName;