Web Programming TutorialsLearn the concept of Models in Zend Framework

Learn the concept of Models in Zend Framework

Models in Zend FrameworkIn 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
  • Now let us try to insert the data entered into the form in the database which is created in MYSQL.
  • Insertion is done using Zend_DB::insert() method.
  • But before any database operation, a connection should be established with the database.
  • For this just see the following code:
  • <?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);
    
        }
    ?>
    
  • In the above code we have rendered the registration.php view file, have created connection to database and then inserted the data into users table.
  • The insertAction method is used to insert data into a table in database.
  • A array named $param is used to group the connection information
  • Next the< strong>Zend_DB class is used to create a connection to the database.
  • The $data array is used to collect the information entered by the user in the registration form.
  • Then the insert method is used to insert the data into the users table, which is collected in $data array as shown below:
  • $DB->insert('users',$data);
  • We have now inserted the data into the database, now let’s retrieve it.
  • The code is given below:
  • 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.";
    }
    
    }
    
    }
    
  • Here in getinfoAction() function we have connected to database as usual and have created a select object called $sel. We have filtered the data by have where condition in the query to match the username and password of the user.
  • We can output the written sql statement using its __toString() method as shown below:
  • $sql = $sel->__toString();
    //echo $sql;
  • To get the query executed the following code is used,
  • $rowsFound = $DB->fetchAll($sel);
    if (isset($rowsFound[0]["unm"]))
    {
       $this->_redirect('/');
    }
    else
    {
       echo "Login information incorrect. Please 
    try again.";
    }
    
  • The fetchAll() method is used to get all the rows from the table, that satisfy the query into a 2D array.
  • So the $rowsFound[0][“unm”] refers to the unm column of the first (zero’th) row.
  • Similarly to update the record, we will have to use Zend_DB::update(tablenm,new_record,row_to_update).
  • In this way we can work with models.

Thus we studied to work with databases in Zend Framework in this Models in Zend Framework tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -