CRUD Zend Framework application in Netbeans.

If You would like to create CRUD application first You must create database with
tables. CRUD means: create read update delete. For these operations  allow
this application. In phpMyAdmin create in MySQL animals database with animal table.
This table has two columns: id and name. Insert into it only one record.

an01Create in Netbeans project crud_animal. Choose from menu File->New Project.

zoo00In New Project window from Categories section select PHP and from Projects section PHP Application.

zoo1Click on Next button.

In Project Name field write crud_animal. Click on Next button. 
an02In next step add on the end of path in Project URL field word: public.

an03

Click on Next button.  Select from list of frameworks Zend PHP Web Framework.

c74

Click on Finish button.

In Projects window in Netbeans IDE You see structure of Your application.

an04

Open application.ini file from application/configs node. Set in production section code to connect to animals database;

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"

an05Save this code.

Create layout for Your applicaton. Right click on application node and choose Zend->Run Command.

an06In Filter field write enable layout and click on Run button.

an07

From application/layouts/scripts node open layout.phtml file and paste into 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>

an08

In this time You may create class for table animals. Right click on application node.

an06In Filter field write create db-table and in Parameters field write Animal ( name of class )  and animal (name of table in animals database ).

an09In Projects window in application/models You see new node DbTable and Animal.php file in it. It is class for animal table.

an10In next step create form for animals, so right click on application node and choose Zend->Run Command.

an06In Filter write create form and in Parameters field write animalform. Click on Run button.

an11In structure application You see new node; forms with Animalform.php file. In this file is only one method init,  which is empty. Paste into it this code:

$this->setMethod('post');
        $text_animal=new Zend_Form_Element_Text('animal_name',array('label'=>'Animal     name:'));
        $this->addElement($text_animal);
        $submit_animal=new Zend_Form_Element_Submit('animal_submit',array('label'=>'Save animal'));
        $this->addElement($submit_animal);

an60

If You have form, may create controllers and actions. Default main controller is  index and its index action. So paste in index controller this code into indexAction method:

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

an13

Go to view so open index action file( views/scrips/index/index.phtml ). Display all records from table animal contains names of animals.
Paste into index.phtml file the code:

<h3>Animals:</h3>
<p>
<?php foreach ( $this->animals as $animal ): ?>
<?php echo $animal['name']." <br/>"; ?>
<?php endforeach;?>

</p>

an34

If You run this application You see list of animals from database.

an15

Then create action for form inserting data to database. Name this action show_form.
Right click on application node and choose Zend->Run Command.

an06

In Filter field write create action and in Parameter write showform index. Click on Run  button.

an19

In views/scripts/index node open showform.phtml file and paste into it code:

 <?php  echo $this->form; ?>

an22

Open index controller and into showformAction  method paste this code:

$this->view->form = new Application_Form_Animalform();
$this->view->form->setAction($this->view->url(array('controller'=>'index','action'=>'createform'),'create'));

an23

In this url You focus to name of action: create. Paste in production section  into application.ini this lines:

resources.router.routes.show.route = "/index/showform"
resources.router.routes.show.defaultController = "index"
resources.router.routes.show.defaultAction = "showform"

resources.router.routes.create.route = "/index/createform"
resources.router.routes.create.defaultController = "index"
resources.router.routes.create.defaultAction = "createform"

And in index view add on the end of file link for adding new animal:

<a href="<?php echo $this->url(array('controller'=>'index','action'=>'showform'),'show') ?>"> add new animal</a>

an33

In this link is call showform action to which reference is show name writing in application.ini file. Run application.

an25

an26

In application.ini definitions createform action of create name. This action get data from form and insert data to database. So first create createform action. Right click on application node and choose Zend->Run Command.

an06

In Filter field write create action and in Parameter field write createform and click on Run button.

an21

Open index  controller and in createformAction  method paste this code:

$request=$this->getRequest();
if ($request->isPost())
{
 $form_animal = new Application_Form_Animalform();
 if ($form_animal->isValid( $request->getPost()) )
    {
      $data = array('name'=>$form_animal->getValue('animal_name') );
      $tab_animal= new Application_Model_DbTable_Animal();
      $tab_animal->insert($data);
       $form_animal->reset();
   }
}
$this->_helper->redirector('index');

an27

In view in createform.phtml You may leave empty or delete this view, because controller call createform action from it redirecting way to index action.  If You after run application active link add new animal You see list of animals with new adding animal.

an30

an31

an32

Your application display animals and add new animal to database. Then allow edit and delete animals.

First create action edit. Right click on application node and choose Zend->Run Command.

an06

 

In Filter field write create action and in Parameters field write edit. Click on Run button.

an35

In the same way create delete action.

an36

And in the same way create update action.

an50

In index.phtml file as main view add two link:  edit and delete actions. Modify code in this file;

<h3>Animals:</h3>
<p>
<?php foreach ( $this->animals as $animal ): ?>
<?php echo $animal['name'].""; ?>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'edit','id'=>$animal['id'])) ?>"> edit </a>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'delete','id'=>$animal['id'])) ?>"> delete </a>
<br/>
<?php endforeach;?>
</p>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'showform'),'show') ?>">add new animal</a>

an43

In index controller paste this code into deleteAction method:

$id = $this->getRequest()->getParam('id');
$tab_animal= new Application_Model_DbTable_Animal();
$animal = $tab_animal->find($id)->current();
$animal->delete();
return $this->_helper->redirector('index');

an46

Clicking on delete link near animal name,  controller call delete action and redirect it to index action. So You may delete delete view( delete.phtml file from views/scripts/index node ).

an45

an47

And there is still to implement edit option. You have edit action and update action. Into edit  view paste this code:

<?php echo $this->form ?>

an52

So open index controller file and paste this code into editAction method:

$id = $this->getRequest()->getParam('id');
$tab_animal= new Application_Model_DbTable_Animal();
$animal = $tab_animal->find($id)->current();
$this->view->form = new Application_Form_Animalform();
$this->view->form->populate($animal->toArray());
$this->view->form->setAction($this->view->url(array('controller'=>'index','action'=>'update','id'=>$id, 'name'=>$animal['name'] ) ) );

an53

And in index controller file  paste this code into updateAction method:

$id = $this->getRequest()->getParam('id');
$name = $this->getRequest()->getParam('animal_name');
$tab_animal= new Application_Model_DbTable_Animal();
$animal = $tab_animal->find($id)->current();
$request=$this->getRequest();
if ($request->isPost())
{
  $data = array('id'=>$id , 'name'=> $name );
  $animal->setFromArray($data);
  $animal->save();
  return $this->_helper->redirector('index');
}

an54

In this moment You may run Your application.

an56

Click on edit link and change name of animal from elephant to cat.

an57

And on the list of animals You see cat instead of elephant.

an58

 

2 thoughts on “CRUD Zend Framework application in Netbeans.

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.