Wednesday, November 6, 2013

Re: Can anybody help me with this postLink() delete?

Thanks els.
You saved my time...

On Wednesday, 8 August 2012 18:24:12 UTC+5:30, els wrote:
Hi amprodes,

I had the same problem. The cookbook states that you should not use postlinks inside another form.
I think removing the Account form will do the trick.

hope this helps,
Els

 
Move the postlink
On Friday, August 3, 2012 3:32:33 PM UTC+2, amprodes wrote:
#### What I did

1.- This is my model:
--------------------------------------------------------------------------------------

<pre>  
 class Account extends AppModel {
public $name = 'Account'; 
public $belongsTo = array(
'Client' => array(
            'className' => 'Client',
            'foreignKey' => 'client_id'
        ),
'Accounttype' => array(
            'className' => 'Accounttype',
            'foreignKey' => 'accounttype_id'
        )
    );
 
public $hasMany = array(
'Event' => array(
            'className' => 'Event',
            'foreignKey' => 'account_id',
'dependent'=> true
        ), 
'Plot' => array(
            'className' => 'Plot',
            'foreignKey' => 'account_id',
'dependent'=> true
        ),
'Widget' => array(
            'className' => 'Widget',
            'foreignKey' => 'account_id',
'dependent'=> true
        )  
);
 
 
}
</pre>

--------------------------------------------------------------------------------------

2.- **This is my controller:**
--------------------------------------------------------------------------------------

<pre>

class AccountsController extends AppController {
    public $helpers = array ('Html','Form');

 
    function index($client_id) {
        $this->set('accounts', $this->Account->find('all', array('recursive' =>1, 'conditions' => 'Account.client_id ='. $client_id)));
$this->set('client_id', $client_id);
    }

public function view($id = null) {
        $this->Account->id = $id;
        $this->set('account', $this->Account->read());
    }
public function add($client_id) { 
        if ($this->request->is('post')) {
            if ($this->Account->save($this->request->data)) {
                $this->Session->setFlash('Your account has been added.'); 
if($this->request->data['Account']['accounttype_id'] == 2){
$this->redirect(array('controller' => 'twitteraccounts', 'action' => 'connect'));
}
            }
        }
 $accounts = $this->Account->find('list'); 
 $accounttype  =  $this->Account->Accounttype->find('list', array('fields'  => array('Accounttype.id', 'Accounttype.description')));
 $this->set('client_id', $client_id);
      $this->set('accounts', $accounts);
 $this->set('accounttype', $accounttype);
    }

function edit($id = null, $client_id) {
    $this->Account->id = $id;
$this->set('id', $id);
    if ($this->request->is('get')) {
        $this->request->data = $this->Account->read();
    } else {
        if ($this->Account->save($this->request->data)) {
            $this->Session->setFlash('Your account has been updated.');
            $this->redirect(array('action' => 'index', $client_id));
        }
    }
 $accounts = $this->Account->find('list'); 
 $accounttype  =  $this->Account->Accounttype->find('list', array('fields'  => array('Accounttype.id', 'Accounttype.description')));
      $this->set('accounts', $accounts);
 $this->set('accounttype', $accounttype);
  $this->set('client_id', $client_id);
}


public function delete($id, $client_id) {
 
    $id = $this->request->params['pass'][0];
   
    //the request must be a post request
    //that's why we use postLink method on our view for deleting user
   if( $this->request->is('get') ){
   
        $this->Session->setFlash('Delete method is not allowed.');
        $this->redirect(array('action' => 'index',$client_id));
       
        //since we are using php5, we can also throw an exception like:
        //throw new MethodNotAllowedException();
    }else{
   
        if( !$id ) {
            $this->Session->setFlash('Invalid id for Account');
            $this->redirect(array('action'=>'index',$client_id));
           
        }else{
            //delete user
            if( $this->Account->delete($id, $cascade = true) ){
                //set to screen
                $this->Session->setFlash('Account was deleted.');
                //redirect to users's list
                $this->redirect(array('action'=>'index',$client_id));
               
            }else{ 
                //if unable to delete
                $this->Session->setFlash('Unable to delete Account.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }
}
}
</pre>

--------------------------------------------------------------------------------------

3.- **This is the view:**
--------------------------------------------------------------------------------------

       echo $this->Html->addCrumb('Clients', '/clients'); 
       echo $this->Html->addCrumb('Accounts', '/accounts'); 
       echo $this->Form->create('Account')
            foreach($accounts as $account): ?>

   <table cellspacing=0 cellpadding=0 style="width:600px">
<tr>
<td>
  <?php echo $account['Accounttype']['description'] ?></br> 
                 </td> 
<td>
                  <?php echo $this->Html->link('Edit', array('action' => 'edit', $account['Account']['id'], $client_id)); ?>
                 </td> 
<td>
                 <?php // echo $this->Html->link('Del', array('action' => 'delete', $account['Account']['id'], $client_id)); ?>
                </td> 
<td>
                <?php echo $this->Html->link('Add Event', '/events/add/'.$account['Account']['id'], array('action' => 'add', $account['Account']['id'])); ?></td>  
<td><?php echo $this->Form->postLink('Delete',array('action' => 'delete', $account['Account']['id'], $client_id),null,  'Are you sure?');?></td>
        </tr>  
</table>
<?php 
endforeach; echo $this->Form->end();
?>
<div style="float:right"><?php  echo $this->Html->link('Back', array('controller' => 'clients', 'action' => 'index')); ?></div>

--------------------------------------------------------------------------------------

What happened

Everything works great but delete button won't delete the last row remaining in accounts list i meant delete works but not in the last row remaining and i think the problem is when the delete postLink() get render, it formed well in all rows but not in the first one the "(form blablabla) (/form)" its not created in the first row and i just get (input blablabla) without form tag so when i click "delete" nothing happens! just get # at the end of the url

What I expected to happen

i expect to get a beautiful blank list saying "there are not accounts assigned to this client" when i delete the last row!

pd: thanks for help me out! and sorry for my bad english!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: