James Walker Jun 9, 2021, 10:00 am EDT | 4 min read Shutterstock/oatawaA programming impedance mismatch occurs when data needs to be transformed into a different architectural paradigm. The most prominent example involves object-oriented codebases and relational databases. An impedance mismatch arises when data is fetched from or inserted into a database. The properties of objects or classes within the codebase need to be mapped to their corresponding database table fields. Mapping and Relationships Your classes won’t necessarily map directly to individual database tables. The construction of an object might require data from several tables to be used in aggregate. You’ll also need to handle relationships between your data. Relational databases make this straightforward by allowing you to reference other records. You can use a JOIN to access all the data encapsulated by the relationship. CREATE TABLE productTypes( ProductTypeUuid VARCHAR(32) PRIMARY KEY, ProductTypeName VARCHAR(255) NOT NULL UNIQUE ); CREATE TABLE products( ProductUuid VARCHAR(32) PRIMARY KEY, ProductName VARCHAR(255) NOT NULL UNIQUE, ProductType VARCHAR(32) NOT NULL, FOREIGN KEY (ProductType) REFERENCES productTypes(ProductTypeUuid) ON DELETE CASCADE ); Using plain SQL, you could get the combined properties of a Product and its ProductType with this simple query: SELECT * FROM products INNER JOIN productTypes ON ProductTypeUuid = ProductType; Then properties of the Product and its ProductType are accessed from the same flat structure: echo $record[“ProductName”]; echo $record[“ProductTypeName”]; This flat array quickly becomes limiting in complex application… Click below to read the full story from How To Geek
Read More