Partial view in Netbeans 7.4 application project in Zend 1.12

In this post I describe how create application project using partial view. Partial view is view to which  link  several another views, so it allows to reference to the same part of code.
If You do it, first create in phpMyAdmin database animals.

zoo10In it, create table group:
zoo11zoo12And create table animal:
zoo13zoo14You must add relational between these tables. The animal table has foreign key to id from table group.
Click in structure of animal table in fk_group row on Index link.
zoo17zoo15zoo16

Insert data into tables. Run this query into SQL command:

insert into `group` (name) values ('mammals');
insert into `group` (name) values ('reptiles');

insert into animal (name,fk_group) values ('lizard',2);
insert into animal (name,fk_group) values ('dog',1);
insert into animal (name,fk_group) values ('cat',1);
insert into animal (name,fk_group) values ('snake',2);
insert into animal (name,fk_group) values ('crocodile',2);
insert into animal (name,fk_group) values ('bear',1);

zoo18

Then You may create project zoo.
Choose from menu File->New Project.
zoo00In Categories section select PHP and from Projects section select PHP Application.
Click on Next button.

zoo1

In next window in Project Name field write zoo and click on Next button.
zoo01In Project URL field add on the end of path word public.
Click on Next button.
zoo03In next step select from list of frameworks Zend PHP Web Framework.
zoo04Click on Finish button.
You see tree your project zoo.
zoo05In this time You may create layout for your application.
So right click on project node and choose Zend->Run Command.
zoo06In Filter field write enable layout and click on Run button.
zoo07Double click on layout.phtml file in layouts/scripts node.
zoo08Paste in it code:

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

and save file.
zoo09

And You configure connect to database animals.
Open application.ini file from application/configs node and paste this code before [staging : production] section but in [production] section:

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

Save file.
zoo20

You may either create classes for tables animal: and group.
So right click on project node and choose from menu Zend->Run Command.
zoo06In Filter field write create db-table and in Parameters field write animal Animals. Click on Run button.
zoo21In the same way create class for group table.
zoo22In models node You see two classes.
zoo23

In this time You must definition relations between tables in their classes.
So in Group.php file paste after $_name line code:

protected $_dependentTables = array( 'Application_Model_DbTable_Animal' );

zoo24And in Animal.php file paste after $_name line code:

protected $_referenceMap = array(
  'Group' => array(
    'columns' => array('fk_group'),
    'refTableClass' => 'Application_Model_DbTable_Group',
    'refTableColumns' => array('id')
                                                    )
                                );

zoo25

For list all animals and list animals of each group You need two actions: allanimals and
animalsgroup and for them new controller listanimals. So create controller right clicking on project node and choosing Zend->Run Command.
zoo06In Filter field write create controller and in Parameters field listanimals. Click on Run button.

zoo26Then create first action. Right click on project node and choose Zend->Run Command.
zoo06In Filter field write create action and in Parameters field allanimals listanimals and click on Run button.
zoo27

And create second action. Right click on project node and choose Zend->Run Command.
zoo06In Filter field write create action and in Parameters field animalsgroup listanimals and click on Run button.
zoo28

In node views/scripts/listanimals delete index.phtml file.
zoo29

Open index view ( views/index/index.phtml ) and paste in it code:

<h3>List of animals</h3>
<p>
<a href="<?php echo $this->url(array(),'all'); ?>">All animals</a><br/>
<a href="<?php echo $this->url(array(),'group_anim'); ?>">Animals divided for groups</a>
</p>

In application.ini set configuration to link to first and second action for listanimals controller.
Paste this code into this file in [production] section:

resources.router.routes.all.route ="/listanimals/allanimals"
resources.router.routes.all.defaults.controller = "listanimals"
resources.router.routes.all.defaults.action = "allanimals"

resources.router.routes.group_anim.route =”/listanimals/animalsgroup”
resources.router.routes.group_anim.defaults.controller = “listanimals”
resources.router.routes.group_anim.defaults.action = “animalsgroup”

Link to ‘all‘ name is reference to listanimals controller and allanimals action and link to
group_anim‘ name is reference to listanimals controller and animalsgroup action.
First create partial view for all another views. Name its animals.
This view is the partial view. Create it opening Run Command window for Zend. Write in Filter field create view and in Parameters field listanimals animals. First parameter is name of controller and second is name of view.
zoo30You see it in views/scripts/listanimals.
zoo31

Drag and drop this file from listanimals folder into scripts.
zoo35In this file paste this code:

<p>
<?php foreach( $this->result_animals as $r): ?>
<?php echo $r['name']; ?>
<br/>
<?php endforeach; ?>
</p>

This code in this partial view display list of animals setting to the array variable result_animals.
zoo32Open listanimals controller and implements methods: allanimalsAction and animalsgroupAction.
In the first method paste this code:

$animals = new Application_Model_DbTable_Animal();
$this->view->animals = $animals->fetchAll();

and in animalsgroupAction method paste code:

$groups = new Application_Model_DbTable_Group();
$this->view->groups = $groups->fetchAll();

zoo33

Open view for allanimals action: allanimals.phtml and paste into it code:

<h3>Animals:</h3>
<?php echo $this->partial('animals.phtml',array('result_animals'=>$this->animals )); ?>

Instead of implements foreach loop You may call partial method and pass into it name of partial view and name of variable in destination view with array of data from controller.
zoo34
Save file and open another view: animalsgroup.phtml.
Paste into this file code:

<h3>Groups</h3>
<?php foreach ($this->groups as $group): ?>
<strong><?php echo $group['name'].':' ?></strong><br/>
<?php echo $this->partial('animals.phtml',array('result_animals'=>$group->findDependentRowset
('Application_Model_DbTable_Animal') )); ?>
<br/>
<?php endforeach; ?>

zoo36

This loop displays from list of group name of group and for it group with findDependentRowset method get all records(animals) referenced to this group. Result from findDependentRowset method as array are set to partial view( to result_animals variable).

It is end Your work. Then You may run application.
zoo37Click on first link. You see all animals selected from animal table from database.
zoo38
And if You click on second link, You see groups with list of its animals.
zoo39

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.