Implementing edit records in multiple associated tables in Cakephp 3

Implementing edit records in multiple associated tables in Cakephp 3

In this blog we are going to discuss how to edit data of associated tables .The most common type error found in this that instead of updating existing records of associated data new records are created .
For example there are 2 tables
Customers  and Customer Orders.

Image removed.

 

 

Image removed.

 

 

Main causes the update fails
1 .   Not defining relationship properly between tables .As in above example the relationships is 1 to many.

So in customersTable.php
Define relationship

Image removed.

 

 

Similary add belongs relationship in OrdersTable.php

Image removed.

 

 

 

The naming of associated table variable in edit.ctp file is important
Taking the eg of above tables .For customer edit.ctp form in templates
We will pass additional value of id of the associated form in this case of customer_orders
Suppose $customerId contains value of id of associated tables than

In the ctp file you will have to create a hidden variable

Image removed.

 

 

Note the naming .If the associated table’s name is CustomerOrders it will be customer_orders.

3.The final step is to patch the entity for associated table

Image removed.

 

 

You can verify by var_dump the customer variable if it has field customer_order and [new] flag is set to false.

That should update the records in associated tables.

Nikhil Kamath
Nikhil Kamath
Software Developer
Tools and Techniques for Being Productive

Be productive using tools and techniques.

Madhav Ranganekar
What Makes You Unique In A Job Interview

What Makes You Unique In A Job Interview

Akshata Alornekar
Time Management for Project Managers

Time Management for Project Managers

ARIF ISLAM