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.
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
Similary add belongs relationship in OrdersTable.php
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
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
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.