In our last tutorial of Zend Framework we studied the use of Calendar class in Zend Framework, today we will learn about Models in Zend Framework in this tutorial.
- A model is a conceptual representation of the entities involved in an application and would be analogous to the class or object in an object oriented world.
- In some places, model contains only the structure of the entity.
- And in some places, it also contains the business logic associated with the object.
- Using models we can interact with the database to insert, retrieve, update and delete records.
- We will take an example of getting registration information from the user through a form and then working operations like inserting, retrieving, updating etc. on it.
- Here I will not give the form code; we will directly see the database interactions.
If your form file name is registration.php, you can browse it with the following address:
http://localhost/user/registration
<?php require_once 'Zend/Db.php'; Zend_Loader::loadClass('Zend_Controller_Action'); Zend_Loader::loadClass('Zend_View'); class RegisterController extends Zend_Controller_Action { function registerAction() { $view = new Zend_View(); $view->setScriptPath('../application/views/'); echo $view->render('registeration.php'); } function insertAction() { $param = array( 'host' => 'localhost', 'username' => 'abcd', 'password' => 'abcd', 'dbname' => 'Register' ); $DB = Zend_Db::factory('PdoMysql', $param); $data = array( 'Fnm' => $_POST['Fnm'], 'Lnm' => $_POST['Lnm'], 'email' => $_POST['email'], 'unm' => $_POST['user'], 'pwd' => new Zend_Db_Expr($db->quoteInto('MD5(?)',$_POST['pass'])), ); $DB->insert('users',$data); } ?>
$DB->insert('users',$data);
class RegisterController extends Zend_Controller_Action { function retrieveAction() { $view = new Zend_View(); $view->setScriptPath('../application/views/'); echo $view->render('info.php'); } function getinfoAction() { $param = array( 'host' => 'localhost', 'username' => 'abcd', 'password' => 'abcd', 'dbname' => 'Register' ); $DB = Zend_Db::factory('PdoMysql', $param); $sel = $DB->select(); $sel = $DB->select(); $sel->from('users', '*'); $sel->where('unm = ?', $_POST['unm']); $sel->where('pwd = ?', $_POST['pwd']); $sql = $sel->__toString(); //echo $sql; $rowsFound = $DB->fetchAll($sel); if (isset($rowsFound[0]["unm"])) { $this->_redirect('/'); } else { echo "Login information incorrect. Please try again."; } } }
$sql = $sel->__toString(); //echo $sql;
$rowsFound = $DB->fetchAll($sel); if (isset($rowsFound[0]["unm"])) { $this->_redirect('/'); } else { echo "Login information incorrect. Please try again."; }
Thus we studied to work with databases in Zend Framework in this Models in Zend Framework tutorial.