Thursday, 15 January 2015

Insert,Delete : DbTable

INSERTING DATA;
Inserting data to the table is very easy. We will create a function called insertData() in the User.php
that will accept an array variable. in that array is the form data. we will further use this form data and insert it into the table using simple insert() method.

here is the code;

public function insertData($data){
    $array = array(
    'firstname' => $data['firstName'],
    'lastname'  => $data['lastName'],
    'email'     => $data['email']  
    );
    $this->insert($array);
    }

here in this function we created an array called $array , set the form data to the firstname, lastname and email. than using simple insert function we called the insert() method and pass it the array. This will add the form data to the table.

Now the question is where is the data coming from? It is actually the form data that we created in the
Zend Form Tutorial. that is submitted to the indexController's IndexAction()
 now we have made some changes to the indexAction() that are;

 public function indexAction()
    {
    if (isset($_POST) && !empty($_POST['firstName'])) {
    $data = $_POST;


    $user = new Application_Model_DbTable_Users();
    $user->insertData($data);
    $this->redirect('index/user');
    }
    else {
    $this->redirect('form/index');
    }
    }

Here we pass the $data variable , that contain the form data to the insertData() method of the user.php file.

when the form is submitted, the data is inserted to the table and we are redirected to the index/user page.

DELETING DATA
Deleting data is also realy easy, we will call the delete() method and pass an id value to it. it will delete the value from the database table.

here is the code of deleteData() function in user.php;

public function deleteData($id){
    $where = $this->getAdapter()->quoteInto('id = ?',$id);
    $this->delete($where);

    }

this function get a parameter from the controller called $id, that is the id of the row of table. where clause matches the id with that in table and delete it form the table.
we have also make some changes to the View file user.phtml,
that looks like that now;

<?php foreach ($this->results as $item):?>
<h1><?php echo $item['firstname'];?></h1>
<h3><?php echo $item['lastname'];?></h3>
<span><?php echo $item['email'];?></span><br/><br/>
<a href="<?php echo $this->baseUrl();?>/index/delete/<?php echo $item['id'];?>">delete</a>

<?php endforeach;?>

a link called delete with every item of the table, when we click on delete link, it is forwarded to the delete action of the indexController.php and from where it called the deleteData() function of the user.php and the item is deleted.
here is the deleteAction() code;

public function deleteAction(){
    $data = $this->_getParam('id');
    $user = new Application_Model_DbTable_Users();
    $user->deleteData($data);
    $this->redirect('index/user');

    }

but there is one problem in the route of the project. to make changes to the url we need to do some changes to the index.php file so that we can get the data form the url.
go to your index.php file  and add these lines at the bottom;


$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'delete',
new Zend_Controller_Router_Route('/index/delete/:id',array('controller'=>'index','action'=>'delete'))
);

now open project in browser and go the index/user  now click the delete link
the item will be deleted and you will be redirected to this page again.
yes you can add a confirmation message if you want, and thats all.


No comments:

Post a Comment