Reputation: 9790
I have a user_addresses
table in database to store user's address. Since user can store many addresses but only one of them will be Primary of Default Address
. To set an address as Primary Address
, table has a column default_flag
which is 1 if primary else 0 for non primary
.
Now since an user can have only one primary address which he can change anytime later. To do so, I listed all addresses where default_flag != 1
and a button next to it to set it as primary address
. When user clicks the button the address is set to primary and previous primary address default_flag
is set to 0
.
I have tried this to do so.
I have created a postButton
with two keys passing to updatePrimaryAddress
action, one is the id of the address which is to set as primary and next is the id of address which is already primary.
<?= $this->Form->postButton(__('Choose'), ['controller' => 'UserAddresses', 'action' => 'updatePrimaryAddress', $nonPrimaryAddress->id, $primaryAddressId], ['class' => 'btn btn-success pull-left']) ?>
In UserAddressesController
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
/**
* UserAddresses Controller
*
* @property \App\Model\Table\UserAddressesTable $UserAddresses
*/
class UserAddressesController extends AppController
{
public function updatePrimaryAddress($id = null, $primaryAddress = null)
{
$userAddresses = TableRegistry::get('UserAddresses');
$primaryAddress = $userAddresses->get($primaryAddress);
$primaryAddress->default_flag = 0;
$userAddresses->save($primaryAddress);
$address = $userAddresses->get($id);
$address->default_flag = 1;
$userAddresses->save($address);
return $this->redirect($this->referer());
}
}
On click of button, the page reloads but no data changes in the database.
Upvotes: 0
Views: 905
Reputation: 9398
Why use postButton when you don't even read the data in your action?
you can use a simple link:
$this->Html->link(__('Choose'), [
'controller' => 'UserAddresses',
'action' => 'updatePrimaryAddress',
$nonPrimaryAddress->id, // you don't need this
$primaryAddressId
],
['class' => 'btn btn-success pull-left']
);
But you don't even need to send the information about the old address as you already have it in your database.
public function updatePrimaryAddress($address_id)
{
// I suppose you have authenticated you user
// so let's retrieve his id
$user_id = $this->Auth->User('id')
// now I set the address as default
// but only if it belongs to that user
$rows_affected = $this->userAddresses->UpdateAll(
['default_flag' => 1],
[
'user_id' => $user_id,
'id' => $address_id
]);
// now I set all the other addresses as not default
// but only if I have successfully changed the default address
// and just for that one user
if($rows_affected > 0)
{
$this->userAddresses->UpdateAll(
['default_flag' => 0],
[
'user_id' => $user_id,
'id !=' => $address_id
]);
}
else
{
$this->Flash->warning("Seems you're trying to switch to a non existing address or to change other's user address");
}
return $this->redirect($this->referer());
}
Upvotes: 1