set_masterdetail() *

* Please note this feature is only available in paid versions.

  • Parameter(s):
    • $obj_grid: phpGrid object as the detail datagrid
    • $fkey: Foreign key to the detail table.
  • Description:
    • This method sets the master detail, or the parent child, relationship between datagrids.
    • The phpGrid went the extra mile to make creating the master detail datagrid a simple task for developers. The primary key value of master datagrid is auomatically retrieved when user selects a row and used as the value to the foreign key in the detail grid. The detail grid renders dynamically based on the row selected in parent grid.
  • Remark (important):
    • The detail grid is a regular phpGrid object and can have the same methods like any other phpGrid, such as description title, sort, and update etc. A master detail can have one or more detail datagrid.
    • The foreign key, or $fkey, does NOT necessarily have to have the same name as the master table primary key, as long as the same name exists in both master and detail tables.
    • When foreign keys don’t have the same name, you should use SQL alias in the master grid. However, if the grid is editable, it’s important to call set_col_readonly() and set the new alias to be read only.
    • By leaving the foreign key field blank in detail grid, master grid will prefill its detail grid with the value of the foreign key before the new record is saved/inserted to the database table. Or simply hide the entire foreign key column (Version 4.5.5 and above only).
  • Example:
1
2
3
4
// orderId exists in both master table(Orders) and detail table(OrderDetails)
$dg = new C_DataGrid("SELECT orderId, orderDate, customerId FROM Orders", "orderNumber", "Orders");
$sdg = new C_DataGrid("SELECT lineId, orderId, productCode, price FROM OrderDetails", "lineId", "OrderDetails");
$dg -> set_masterdetail($sdg, 'orderId');

When foreign keys don’t have the same name, you can use SQL alias in the master grid. Edit will fail when using SQL alias in detail grid. But also remember to set the alias to be read only if the grid is editable.

1
2
3
4
5
// productId is SQL alias to productNo in master table(ProductParts), then used as foreign key to link to detail table(Products).
$dg = new C_DataGrid("SELECT partNo, partName, productNo AS productId FROM ProductParts", "partNo", "ProductParts");
$dg -> enable_edit('INLINE', 'CRUD')->set_col_readonly("productId");
$sdg = new C_DataGrid("SELECT productId, productName, productDescription FROM Products", "productId", "Products");
$dg -> set_masterdetail($sdg, 'productId');

Still having trouble editing master detail grids with column name alias?
Please see Master detail grid with column name alias

No related content found.

Tags: