【问题标题】:zend 2 getting entity properties value without using in exchangearray() methodzend 2 在不使用 exchangearray() 方法的情况下获取实体属性值
【发布时间】:2014-05-14 06:26:03
【问题描述】:

我是zend framewrok 2的新手。我只是在练习zf2文档提供的相册应用程序。之后我创建了另一个控制器,即 PersonalInfo。在 PersonalInfo 实体中,我使用了一个属性,即 dt,它不会被表单输入更新。它默认在数据库中更新。这是我的实体。

class PersonalInfo {
protected $id;
protected $name;
protected $fName;
protected $email;
protected $picture;
protected $dt;


public function exchangeArray($data) {
    $this->id = (!empty($data['id'])) ? $data['id'] : null;
    $this->name = (!empty($data['name'])) ? $data['name'] : null;
    $this->fName = (!empty($data['fName'])) ? $data['fName'] : null;
    $this->email = (!empty($data['email'])) ? $data['email'] : null;
    $this->picture = (!empty($data['picture'])) ? $data['picture'] : null;

}

// Add the following method:
public function getArrayCopy() {
    return get_object_vars($this);
}
public function getId() {
    return $this->id;
}

public function setId($id) {
    $this->id = $id;
}

public function getName() {
    return $this->name;
}

public function setName($name) {
    $this->name = $name;
}

public function getFName() {
    return $this->fName;
}

public function setFName($fName) {
    $this->fName = $fName;
}

public function getEmail() {
    return $this->email;
}

public function setEmail($email) {
    $this->email = $email;
}

public function getPicture() {
    return $this->picture;
}

public function setPicture($picture) {
    $this->picture = $picture;
}
public function getDt() {
    return $this->dt;
}

public function setDt($dt) {
    $this->dt = $dt;
}

如果我不使用 $this->dt= (!empty($data['dt'])) 我很奇怪? $data['dt'] : null;在 exchangeArray 方法中 getDt() 方法返回 null 但如果我在 exchangeArray 方法中使用它而不是返回实际数据。在我的一般理解中,我不清楚 exchangeArray 方法的技巧。我有必要知道,因为将来要解决这类问题。

【问题讨论】:

  • 参考文档,exchangeArray() 方法基本上用于将 $_POST 值设置为对象属性。 (注意:不必总是发布值)。

标签: php zend-framework2


【解决方案1】:

如果您查看http://framework.zend.com/manual/2.0/en/user-guide/database-and-models.html#using-servicemanager-to-configure-the-table-gateway-and-inject-into-the-albumtable,您会注意到所有这些信息都是在您的工厂中定义的

'AlbumTableGateway' => function ($sm) {
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
    $resultSetPrototype = new ResultSet();
    $resultSetPrototype->setArrayObjectPrototype(new Album());
    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
}

定义中感兴趣的部分是new ResultSet()setArrayObjectPrototype() 方法

如果您查看\Zend\Db\ResultSet\ResultSet,您会发现exchangeArray 方法是必需的-> https://github.com/zendframework/zf2/blob/master/library/Zend/Db/ResultSet/ResultSet.php#L65

这个要求的原因是,当你迭代你的结果集时,ResultSet 本身会克隆你的原型,然后调用exchangeArray,它有效地用行数据“水合”你的对象 -> https://github.com/zendframework/zf2/blob/master/library/Zend/Db/ResultSet/ResultSet.php#L105

因此,总而言之,当以上述方式使用时,如果您没有在 exchangeArray 方法中显式设置所有属性,则从数据库中获取时它们将始终保持为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多