Database application in Zend Framework 1.12.3 in Netbeans 7.4 get data from several relational tables. – part 2

In last post You create Zend project: magazine. Lets open in Your project application.ini file( application/configs folder). In production part paste this code:

resources.db.adapter = "pdo_mysql"
resources.db.params.dbname = "magazine"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "dorota"
resources.db.params.charset = "utf8"

g1Save this file. Here create layout for Your website.
Right click on project node magazine and select from pop-up menu Zend->Run Command.
e31
In Filter field write enable layout and click on Run button.
e32Open file: application/layouts/scripts/layout.phtml and paste in it code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>News</title>
</head>
<body>
<h1>News</h1>
<?php echo $this->layout()->content; ?>
</body>
</html>

e33In this time You may create another action for index controller. You have only index action.
Add authors action.
Right click on magazine node and choose from pop-up menu Zend->Run Command. In Filter field write create action and in Parameters field write authors index.
e34Click on Run button.
In application/controllers/IndexController.php You see new method authorsAction.
e35And in application/views/scripts/index You see authors.phtml file as view for index controller.

e36Then You create 3 clasess for 3 tables from database: author, article, news.

Open Run Command Zend windows and in Filter field write:
create db-table
and in Parameters:
Author author
and click on Run button.
e37In the same way write command:
create db-table
Article article
e38and
create db-table
News news

e39In node models/DbTable You see new php files for tables.
e40For table author in node models/DbTable create Author folder. In it You create Row.php file.
For table article in node models/DbTable create Article folder.
So right click on DbTable node and choose New->Folder.

e45In New Folder window write in Folder Name: Author and click on Finish button.

e46Right click on Author folder and choose New->PHP Class.
e47In New PHP Class window write in File Name field Row and click on Finish button.
e48In Row.php in author catalogue paste this code:

<?php
class Application_Model_DbTable_Author_Row extends Zend_Db_Table_Row
{
public function __toString()
{
return $this->name;
}
}

Save this file.
e49This class have only one method __toString. In it is implemented return value as author’s name.
So if You display row value You see author’s name. Word name for this object is name of the
column in author table.
In the same way implement Row.php file for article table, paste this code:

<?php
class Application_Model_DbTable_Article_Row extends Zend_Db_Table_Row
{
public function __toString()
{
return $this->title;
}
}

e50In tree project structure You see:
e51

Open Author.php file and write after $_name variable code:

protected $_rowClass = 'Application_Model_DbTable_Author_Row';
protected $_dependentTables=array('Application_Model_DbTable_News');

e41In Article.php file paste this code:

protected $_rowClass = 'Application_Model_DbTable_Article_Row';
protected $_dependentTables=array('Application_Model_DbTable_News');

e42

This code say You that from this table depend another table: news, because news contains
foreign key as id from this table. Variable $_rowClass say that in this project is implemented
your own Row class.
Open News.php file from models/DbTables node. And after $_name variable write code:

protected $_referenceMap = array(
'Article' => array(
'columns' => array('id_article'),
'refTableClass' => 'Application_Model_DbTable_Article',
'refTableColumns' => array('id')
),
'Author' => array(
'columns' => array('id_author'),
'refTableClass' => 'Application_Model_DbTable_Author',
'refTableColumns' => array('id')
)
);

In this variable are two arrays with index as name of the table, which primary keys table news is storage.
For each name of table is assign array of class for this table, its class file and name of
column with primary key.

Open IndexController.php file from application/controller node. In indexAction method write this code:

$Article = new Application_Model_DbTable_Article();
$this->view->articles = $Article->fetchAll();

and in authorsAction method write this code:

$Author = new Application_Model_DbTable_Author();
$this->view->authors = $Author->fetchAll();

In each method is created instance class of the table from database: author and article.
For each these two objects are run fetchAll method. This method return all records from table.
This result is given to view for the action.
e44So You may refer to views. First open index.phtml file from application/views/scripts/index
node.
This view will be display list of articles. Paste in this place code:

<h3>Articles</h3>
<?php foreach ($this->articles as $article): ?>
<strong><?php echo $article.':'; ?></strong><br/>
<?php foreach ($article->findManyToManyRowset
('Application_Model_DbTable_Author','Application_Model_DbTable_News') as $author): ?>
&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $author; ?><br/>
<?php endforeach; ?>
<?php endforeach; ?>

Save this file.
e60Method findManyToManyRowset references to indirect table news and another related table with its key in this table news.
In next step in authors.phtml file paste this code:

<h3>Authors</h3>
<?php foreach ($this->authors as $author): ?>
<strong><?php echo $author.':'; ?></strong><br/>
<?php foreach ($author->findManyToManyRowset
('Application_Model_DbTable_Article','Application_Model_DbTable_News') as $article): ?>
&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $article; ?><br/>
<?php endforeach; ?>
<?php endforeach; ?>

And save this file.
e61In application.ini file ( application/configs node) write route for url for actions.

resources.router.routes.articles.route = "/index/index"
resources.router.routes.articles.defaultController = "index"
resources.router.routes.articles.defaultAction = "index"

resources.router.routes.authors.route = "/index/authors"
resources.router.routes.authors.defaultController = "index"
resources.router.routes.authors.defaultAction = "authors"

e62

First 3 lines are for index action index controller. It’s name is articles.
Second 3 lines are for authors action index controller. It’s name is authors.

In layout.phtml file add code with links for authors and articles websites.

<p>
<a href="<?php echo $this->url(array('controller'=>'index'),'articles') ?>">articles</a>
<a href="<?php echo $this->url(array('controller'=>'index'),'authors') ?>">authors</a>
</p>

In url You give controller name in array and name of route for action.
e63

In the moment You may try run Your application. Right click on magazine node and choose Run.
e64You see in browser website with list of articles and its authors.
e65If You click on authors link You see list of authors with his written articles.
e66

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.