Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Abstract row gatewayset from array #2950

Merged
merged 3 commits into from Jan 21, 2013

Conversation

Projects
None yet
4 participants
Contributor

odnanref commented Nov 12, 2012

setFromArray This is pratically the same method used in ZF1 ported to ZF2.

The diference is I used offsetSet to assign the values.

@ghost ghost assigned ralphschindler Nov 19, 2012

@samsonasik samsonasik and 1 other commented on an outdated diff Nov 21, 2012

library/Zend/Db/RowGateway/AbstractRowGateway.php
@@ -353,4 +353,21 @@ protected function processPrimaryKeyData()
}
}
+ /**
+ * Set all Data in the row from a Array
+ *
+ * @param array $data
+ * @return AbstractRowGateway
+ */
+ public function setFromArray( array $data )
+ {
@samsonasik

samsonasik Nov 21, 2012

Contributor

no space after ( and after $data. it should be :

setFromArray(array $data)
@odnanref

odnanref Nov 22, 2012

Contributor

fixed

@samsonasik samsonasik and 1 other commented on an outdated diff Nov 21, 2012

tests/ZendTest/Db/RowGateway/AbstractRowGatewayTest.php
+ public function testsetFromArray()
+ {
+ $this->rowGateway = $this->getMockForAbstractClass( 'Zend\Db\RowGateway\AbstractRowGateway');
+
+ $mockSql = $this->getMockForAbstractClass('\Zend\Db\Sql\Sql', array( $this->mockAdapter ));
+
+ $rgPropertyValues = array(
+ 'primaryKeyColumn' => "id",
+ 'table' => 'foo',
+ 'sql' => $mockSql
+ );
+ $this->setRowGatewayState( $rgPropertyValues );
+ $this->rowGateway->populate( array( "id" => 4, "name" => "Zender" ) );
+ $this->rowGateway->setFromArray( array( "name" => "Zender Version2" ) );
+
+ $this->assertEquals( "Zender Version2", $this->rowGateway->offsetGet( "name" ) );
Member

ralphschindler commented Jan 4, 2013

Why do the array_intersect_key() here? This assumes that values have been previously set, right?

Owner

weierophinney commented Jan 21, 2013

@ralphschindler array_intersect_key() returns all values of the first array that have keys in the subsequent arrays. As presented in this patc, this ensures that only keys that are represented by the row are present in the set, which allows the offsetSet() not to raise exceptions for invalid keys.

Looks sane - I'll merge.

@weierophinney weierophinney merged commit 5257b17 into zendframework:develop Jan 21, 2013

1 check passed

default The Travis build passed
Details
Owner

weierophinney commented Jan 21, 2013

Reverted. @ralphschindler pointed out that a method already exists for this, populate(), which is more robust in terms of primary key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment