1

I am newbie to zend framewrok 2. I am just practicing album application provided by zf2 documentation. After that i create another controller namely PersonalInfo. In PersonalInfo entity i used a properties namely dt which will be not updated by form input. It is updated by default in database. Here is my Entity.

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;
}

I am strange if i not use $this->dt= (!empty($data['dt'])) ? $data['dt'] : null; in exchangeArray method then getDt() method return null but if i use that in exchangeArray method than it returns actual data. In my general understanding i am not clear about the trick of exchangeArray method. It is neccessary to know for me because in future to solve this type of problem.

1
  • With reference to the documentation, exchangeArray() method is basically shown to be used for setting the $_POST values to the object properties. (Note: not necessary to be post values always). Commented May 14, 2014 at 9:55

1 Answer 1

1

If you take a look at 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 you'll notice that all this information is defined in your factory

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

The part of interest in the definition is the new ResultSet() and the setArrayObjectPrototype() method

If you take a look at \Zend\Db\ResultSet\ResultSet you'll see that the exchangeArray method is required -> https://github.com/zendframework/zf2/blob/master/library/Zend/Db/ResultSet/ResultSet.php#L65

The reason for that requirement is because, as you iterate over your result set, the ResultSet itself clones your prototype, and then calls exchangeArray which effectively 'hydrates' your object with row data -> https://github.com/zendframework/zf2/blob/master/library/Zend/Db/ResultSet/ResultSet.php#L105

So, in summary, when used in the above manner if you don't explicitly set all your properties in the exchangeArray method, they will always remain null when fetched from the db.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.