You're asking about using TArrayData to manage multiple invoice detail records for MariaDB, similar to how TDataRow handles single records. This would create an ORM-like batch editing capability where you can edit multiple detail records in memory and save them all at once to MariaDB.
Yes, TArrayData is exactly the right class for this use case. tarrdata.prg:42 The class is designed to handle arrays of data with full CRUD operations and can work directly with MariaDB connections for batch operations.
How TArrayData Works with MariaDB
TArrayData supports MariaDB through its constructor that accepts a MariaDB connection: tarrdata.prg:265-266
The class can be instantiated in several ways for MariaDB:
New( oCn, cTable, [cWhere] ) - Load records from a MariaDB table tarrdata.prg:247
New( oCn, cSql, [aParams] ) - Load from a complex SQL query tarrdata.prg:248
Key Features for Your Use Case
Batch Editing and Saving: TArrayData provides SaveData() method that saves all modifications, additions, and deletions in a single transaction: tarrdata.prg:223
MariaDB Transaction Support: The SaveToMYSQL() method handles all changes within a transaction, ensuring data consistency: tarrdata.prg:1719-1733
TDataRow Integration: You can create TDataRow objects from TArrayData for individual record editing: tarrdata.prg:214-217
Example Implementation
Here's how you would implement this for invoice details:
// Load invoice detail records
oInvDetails := TArrayData():New( oCn, "invoice_details", "invoice_id = " + Str( nInvoiceId ) )
// Enable auto-append for easy adding of new detail lines
oInvDetails:lAutoAppend := .t.
// Set up XBrowse for editing
oBrw := TXBrowse():New( oDlg )
oInvDetails:SetXBrowse( oBrw )
// Edit individual records using TDataRow
oDetailRec := oInvDetails:Record( "item_code,description,qty,price" )
oDetailRec:Edit()
// Save all changes (edits, additions, deletions) in one transaction
if oInvDetails:SaveData()
MsgInfo( "All changes saved successfully" )
else
MsgAlert( "Error saving changes" )
endif
Integration with Master Record
You can use this alongside your master invoice TDataRow:
// Master invoice record
oInvoice := TDataRow():New( oCn:RowSet( "invoices", "id = " + Str( nInvoiceId ) ) )
// Detail records array
oInvDetails := TArrayData():New( oCn, "invoice_details", "invoice_id = " + Str( nInvoiceId ) )
// Edit master
oInvoice:Edit()
// Edit details in browse
// ... user edits in XBrowse ...
// Save both
if oInvoice:Save() .and. oInvDetails:SaveData()
MsgInfo( "Invoice saved completely" )
endif
Advanced Features
Primary Key Management: TArrayData automatically handles primary keys for MariaDB tables: tarrdata.prg:1725-1728
Field Defaults: You can set default values for new records: tarrdata.prg:52
Change Tracking: The class tracks all modifications and provides lModified property: tarrdata.prg:81-83
Notes
This approach provides exactly the ORM-like functionality you're looking for. The TArrayData class was specifically enhanced in recent versions to support this kind of batch editing with MariaDB, as documented in the changelog: whatsnew.txt:3660-3670 The class handles transactions automatically, ensuring data integrity when saving multiple related records.