For simple application and to get your feet wet in ZF start out by just using the DbTable models that tie your database table to the database adapter. This is not best practice, buit is easy and will get you started.
using the Zend_Tool cli the command will have this format
zf create db-table name actual-table-name module force-overwrite
,
which will translate to:
zf create db-table Users users
db
this will create a file named Users.php
at /application/models/DbTable/
and it will look like:
<?php
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users'; //name of table, does not have to match name of class
}
now to use this in a controller to fetchAll
is as simple as:
<?php
class IndexController extends Zend_Controller_Action {
public function indexAction(){
$db = new Application_Model_DbTable_Users();//instantiate model
$result = $db->fetchAll();//perform query, see Zend_Db_Table_Abstract for API
$this->view->users = $result;//send to view
}
}
just by making this one little class you will have access to the functionality of your chosen database adapter. You can also build methods in the DbTable model to customize your access needs.
<?php
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users'; //name of table, does not have to match name of class
public function getUser($id) {
$select = $this->select();//see Zend_Db_Select for info, provides more secure interface for building queries.
$select->where('id = ?', $id);//can be chained
$result = $this->fetchRow($select);//returns a row object, if array is needed use ->toArray()
return $result;
}
}
This method would be used in a similar manner to the fetchAll() method:
<?php
class IndexController extends Zend_Controller_Action {
public function indexAction(){
$id = $this->getRequest()->getParam('id');//assumes the id is set somehow and posted
$db = new Application_Model_DbTable_Users();//instantiate model
$result = $db->getUser($id);//perform query, see Zend_Db_Table_Abstract for API
//$this->view->user = $result;//send to view as object
$this->view->user = $result->toArray();//send to view as an array
}
}
Hope this get you started, don't forget to read the manual