ZF DB Connectivity


 

ZF DB Connectivity

In this current tutorial we will study how to connect with the database and how to retrieve values from the database. Now the connectivity with database, declaring table etc. and other activities are quite different....

In this current tutorial we will study how to connect with the database and how to retrieve values from the database. Now the connectivity with database, declaring table etc. and other activities are quite different....

ZF Database Connectivity:

In this current tutorial we will study how to connect with the database and how to retrieve values from the database.

The connectivity with database, declaring table etc. and other activities like retrieving values are quite different from the conventional method. So go through each step carefully and you will come to know in proper way.

Let's get started:

First of all we need to establish the connection with the database, to do this we need to add few lines in the application.ini (which is present in configs folder), now add the following lines at the end of [production] section (above [staging] section):

resources.db.adaptor=PDO_MYSQL

resources.db.params.host=localhost

resources.db.params.username=root

resources.db.params.password=

resources.db.params.dbname=rose

In the above settings we set the adaptor as mysql, host as localhost and so on. Change the above values with your dbname and other. In the current tutorial we assume that you know how to create database, table, and how to insert values into it, so we skip that part.

Now we will make some necessary changes in the model section of the project. Since this is the business logic part of the project that's why ZF does not provide any Zend_Model class. In this tutorial we will create a class that will extends the Zend_Db_Table . We will extend the Zend_Db_Table_Abstract. We call our class as Application_Model_DbTable_Albums

<?php

class Application_Model_DbTable_Students extends Zend_Db_Table_Abstract

{

protected $_name='students';

public function getStudent($id){

$id=(int)$id;

$row=$this->fetchRow('id ='.$id);

if(!$row)

{

throw new Exception ("Could not find row $id");

}

return $row->toArray();

}

}

..

Next replace the following code with the IndexController.php's indexAction()'s existing code:

0

....

public function indexAction()

{

1

$this->view->title="Index Page";

$this->view->headTitle($this->view->title);

$students=new Application_Model_DbTable_Students();

2

$this->view->students=$students->query();

}

....

3

Now replace (if any code previously exist) the following codes with the existing code of the index.phtml:

<table>

<th>Name</th>

4

<th>Address</th>

<?php foreach ($this->students as $student):?>

<tr>

5

<td><?php echo $this->escape($student->name);?></td>

<td><?php echo $this->escape($student->address);?></td>

<td><?php echo $this->escape($student->objective);?></td>

6

</tr>

<?php endforeach;?>

</table>

7

 

And finally the output will be as follows:

ZF Database

8

 

Ads