ZF2-502: AbstractRowGateway

Description

Hello, I'm writing to notice what seems to be an issue in AbstractRowGateway

The code bellow in line 104


public function populate(array $rowData, $rowExistsInDatabase = false)
    {
        $this->initialize();

        $this->data = $rowData;
        if ($rowExistsInDatabase == true) {
            $this->processPrimaryKeyData();
        } else {
            $this->primaryKeyData = null;
        }

        return $this;
    }

When called to populate the object with existing data from the database the call is:


$row = new RowGateway("id", "supplier", $adapter);
$row->populate((array)$someRow, true);

when changing the data the call would be:


$row->populate((array)$someformValues);

The issue is that if you do this the second call will clear the primary Key fields


$row->populate((array)$someformValues);

So, my point is that the else block should be removed from this. If a new row is to be created or the value cleared the "user" should insert new values.

Best regards, FR

PS: The Unknown macro seems to be a issue of the parser in this wiki system.

Comments

populate() is not really for "changing data". As you can see, $this->data = $rowData; every time populate is called. If you want to change data (from an array), we really need a separate method for that, or alter the data row-by-row via the object or array interface for the object.

Ok, your correct.

But then can we just add a new method for this...

public function setFromArray(array $newData ) { foreach ($newData as $k => $v ) { if (array_key_exists($k, $this->data)){ $this->data[$k] = $v; } } return $this; }

This is copying a method from ZF1 not sure if it's exactly like this in zf1. Anyway just talking.

BR, André

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2544