Learn the concept of Models in Zend Framework

0
1550
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:

  • 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:
  • 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:
  • We have now inserted the data into the database, now let’s retrieve it.
  • The code is given below:
  • 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:
  • To get the query executed the following code is used,
  • 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