Home Tutorial Php Zendframework ZF DB Connectivity

 
 

Share on Google+Share on Google+
ZF DB Connectivity
Posted on: May 5, 2010 at 12:00 AM
Advertisement
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:

....

public function indexAction()

{

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

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

$students=new Application_Model_DbTable_Students();

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

}

....

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

<table>

<th>Name</th>

<th>Address</th>

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

<tr>

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

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

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

</tr>

<?php endforeach;?>

</table>

 

And finally the output will be as follows:

ZF Database

 

Advertisement

Related Tags for ZF DB Connectivity:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 5, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com