Creating mapped models
This is a useful tip for avoiding headaches when you are creating models mapped to tables with laminas-db
. These models need to implement two methods, that is, exchangeArray
and getArrayCopy
:
public function exchangeArray($data)
The exchangeArray
method is used by the Laminas\Db\ResultSet
class to populate a model with data from a table record.
public function getArrayCopy()
The getArrayCopy
method is used by the Laminas\Db\ResultSet
class to get data from a model. It is also used by Laminas\Form\Form
to bind model attribute values to form field values.
In fact, the Laminas\Db\ResultSet
class also accepts a toArray
method, replacing the getArrayCopy
method. I suggest implementing the toArray
method in models to extract the data to be persisted with the TableGateway
class. Sometimes, the model data to be sent to a form object is different from the model data to be sent to a data mapper. You can show, in an HTML form, data that won’t be saved...