我将 PHP 与 ZF2 结合使用,在其模型文件中始终有一个名为 exchangeArray()
的函数。
该函数的主要职责是通过数据数组填充对象属性:
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
当所有数据库函数都处理数据数组而不是对象时,这个函数是做什么用的?我们应该调用它还是在需要时自动调用它?!!!
最佳答案
exchangeArray()
方法是表单水合器的必需条件;特别是 Zend\Stdlib\Hydrator\ArraySerialized
Hydrator,以便在从表单字段绑定(bind)时可以访问域对象的 protected 属性。
Zend\Stdlib\Hydrator\ArraySerializable - Follows the definition of
ArrayObject
. Objects must implement either theexchangeArray()
orpopulate(
) methods to support hydration, and thegetArrayCopy()
method to support extraction.
关于php - ZF2 模型中的exchangeArray函数是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26991170/