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.

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

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

Click on Next button.

In Project Name field write crud_animal. Click on Next button. 
In 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"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "dorota"
resources.db.params.charset = "utf8"

Save this code.

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

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

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


 
  Animals
 
 
 
 
 

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

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

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

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

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

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

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:

Animals:






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:

 

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

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: