Helping ordinary people create extraordinary websites!

Zend Framework Tutorial

By Lyndon Baptiste
2008-08-13

Back to Zend_DB_Table

Let's add a list action to IndexController.php. I'm assuming that the below code is pasted starting from line 55 of IndexController.php:

public function listAction() {
//include model
require_once 'models/User.php';
$user = new User();
$userInfo = $user->info();
//print_r($userInfo); exit;

$this->view->rsUserList = $user->fetchAll(null, 'email');
$this->render();
}

The model is included and an object created. While line 59 is of no consequence to this particular action it demonstrates a method that is crucial to development. The info method returns an array of information specific to the database table to which it has been mapped. That means you can access the name of the database table (handy if you can't decide on a standard for naming tables in the office) and mappings of database fields to columns (just ensure that the fields never change position if you choose to use an index to access fields rather than their names). Pressing on, line 62 makes a call to an instance method fetchAll. All parameters of this method are optional. The first can be a string which specifies a where clause. The second is the field which you wish to sort the table by. This can be a comma separated list postfixed by "asc" or "desc" depending on the order you wish to sort by. It is important to note that a list of objects are returned from this method therefore fields are accessed using the -> notation. For programmers wishing an array of results a subsequent call can be made to the toArray() method. For example:

$user->fetchAll()->toArray();

The list is registered with the view object so it can be accessed from list.phtml, for which the code is pasted below:

<?php
if (count($this->rsUserList) > 0) {
echo '<table class="results">';
echo '<tr>';
echo '<th>Email</th>';
echo '<th>Gender</th>';
echo '</tr>';

foreach ($this->rsUserList as $rsURow) {
echo '<tr>';
echo '<td>'.$rsURow->email.'</td>';
echo '<td>'.strtoupper($rsURow->gender).'</td>';
echo '</tr>';
}
echo '</table>';
}
else {
echo '<p>No records found</p>';
}
?>

You may be wondering why in our phtml scripts our registered variables aren't being accessed as $this->view->rsUserList. It's because the script is being rendered within the context of the default view object therefore elements must be accessed via the this keyword. The script is straightforward at this point performing a count on the number of records returned. If it is greater than zero then the list of objects are traversed. The name of the instance variables will map to the name of the fields in the database making this approach ideal for data driven applications. Figure 6 shows what the formatted results would look like.





Tutorial pages:
 10 Votes

You might also want to check these out:


Leave a Comment on "Zend Framework Tutorial"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS