877-GRID-666 24/7 online support 604, Tower A, New Trade Plaza, N.T., Hong Kong

set_masterdetail() *

* Please note this feature is only available in phpGrid Professional and Enterprise.


  • Parameter(s):
    • $obj_grid: phpGrid object as the detail datagrid
    • $fkey: Foreign key of the detail grid to the master datagrid.
    • $m_fkey: Foreign key of the master grid to the detail grid. It’s optional when $fkey and $m_fkey have the same column name. (supported since version 7.1)
  • 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 automatically retrieved when a user selects a row. The value is used as the foreign key for the detail datagrid. The detail datagrid renders dynamically based on the row selected in the master 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 seike>e new alias to be read only. the 3rd parameter to reference to the foreign key from the master datagrid.
    • 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:

When the master detail reference keys have the same name,  the 3rd parameter can be omitted.

// 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');


Use the 3rd parameter when the keys to reference have different name in master and detail tables.

// 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 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', 'productNo');

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