mbenjemaa
mbenjemaa

Reputation: 79

Cakephp update another table on change of a field

I have 2 tables: Clients and AssetsAssignations. Clients table has a field: is_active.

In edit mode, when is_active is changed from TRUE to FALSE, I need to update Assignations table by setting end_date=TODAY for all assets_assignations of the selected client.

AssetsAssignations contains these fields: (id, asset_id, client_id, room_id, ip_address, starting_date, ending_date). In AssetsAssignationsTable:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('assets_assignations');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsTo('Assets', [
        'foreignKey' => 'asset_id'
    ]);
    $this->belongsTo('Clients', [
        'foreignKey' => 'client_id'
    ]);
    $this->belongsTo('Rooms', [
        'foreignKey' => 'room_id'
    ]);

}

In ClientsTable:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('clients');
    $this->DisplayField('full_name');
    $this->primaryKey('id');

    $this->hasMany('AssetsAssignations', [
        'foreignKey' => 'client_id'
    ]);

    ....
}

This is my edit function for Clients:

public function edit($id = null)
{
    $client = $this->Clients->get($id, [
        'contain' => []
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $client = $this->Clients->patchEntity($client, $this->request->data);
        if ($this->Clients->save($client)) {
            $this->Flash->success(__('The client has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The client could not be saved. Please, try again.'));
    }
    $clientTypes = $this->Clients->ClientTypes->find('list', ['limit' => 200]);
    $this->set(compact('client', 'clientTypes'));
    $this->set('_serialize', ['client']);
}

Upvotes: 0

Views: 194

Answers (1)

Greg Schmidt
Greg Schmidt

Reputation: 5098

My recommendation:

In ClientsTable.php, add

use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\I18n\FrozenDate;

/**
 * Perform additional operations after it is saved.
 *
 * @param \Cake\Event\Event $event The afterSave event that was fired
 * @param \Cake\Datasource\EntityInterface $entity The entity that was saved
 * @param \ArrayObject $options The options passed to the save method
 * @return void
 */
public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) {
    // For CakePHP 3.4.3 or later, use isDirty instead of dirty
    if (!$entity->isNew() && $entity->dirty('is_active') && !$entity->is_active) {
        $this->AssetsAssignations->updateAll(['end_date' => FrozenDate::now()], ['client_id' => $entity->id]);
    }
}

By placing this code in the afterSave event handler, you are ensured that it will happen any time the is_active flag is changed to false, whether that change comes from the edit page or elsewhere. (There may not be anywhere else that updates this now, but this future-proofs you in case some such thing is added later.)

Upvotes: 1

Related Questions