php - ZF2 模型中的exchangeArray函数是什么

标签 php zend-framework2

我将 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 属性。

来自the documentation

Zend\Stdlib\Hydrator\ArraySerializable - Follows the definition of ArrayObject. Objects must implement either the exchangeArray() or populate() methods to support hydration, and the getArrayCopy() method to support extraction.

关于php - ZF2 模型中的exchangeArray函数是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26991170/

相关文章:

php - 带有 Firefox 和不允许的关键字符的 CodeIgniter

postgresql - Doctrine2 使我对 OneToMany 和 ManyToOne 关系感到疯狂

doctrine-orm - 原则 2 中没有为实体指定标识符/主键

php - 如何使用 php 从 mysql 数据库按年份显示条形图值

java - Quercus 是 Java 环境中 PHP 的可行替代品吗?

php - 在 Laravel 4 中更改日志级别

php - array_unshift 用于多维数组

php - Zend Framework 2 - Doctrine 2 - 如何处理 Zend 标准模型

zend-framework2 - Zend Framework-2 : Best way to protect all Controllers?(ZfcUser/HybridAuth)

command-line - 如何在 zenframework 2 上配置 doctrine 命令行工具