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.


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


Click on Finish button.

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


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" = "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.


From application/layouts/scripts node open layout.phtml file and paste into it code:

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


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:

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


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();


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:

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



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


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


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


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

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


Open index controller and into showformAction  method paste this code:

$this->view->form = new Application_Form_Animalform();


In this url You focus to name of action: create. Paste in production section  into application.ini this lines: = "/index/showform" = "index" = "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>


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



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.


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


Open index  controller and in createformAction  method paste this code:

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();


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.




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.



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


In the same way create delete action.


And in the same way create update action.


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

<?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>
<?php endforeach;?>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'showform'),'show') ?>">add new animal</a>


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();
return $this->_helper->redirector('index');


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 ).



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 ?>


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->setAction($this->view->url(array('controller'=>'index','action'=>'update','id'=>$id, 'name'=>$animal['name'] ) ) );


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();
if ($request->isPost())
  $data = array('id'=>$id , 'name'=> $name );
  return $this->_helper->redirector('index');


In this moment You may run Your application.


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


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



Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.