/** * @covers Zend\Db\RowGateway\RowGateway::save */ public function testSaveUpdate() { // test update $this->mockResult->expects($this->any())->method('current')->will($this->returnValue(array('id' => 6, 'name' => 'foo'))); $this->rowGateway->populate(array('id' => 6, 'name' => 'foo'), true); $this->rowGateway->save(); $this->assertEquals(6, $this->rowGateway['id']); }
/** * @covers Zend\Db\RowGateway\RowGateway::save */ public function testSaveUpdateChangingPrimaryKey() { // this mock is the select to be used to re-fresh the rowobject's data $selectMock = $this->getMock('Zend\\Db\\Sql\\Select', array('where')); $selectMock->expects($this->once())->method('where')->with($this->equalTo(array('id' => 7)))->will($this->returnValue($selectMock)); $sqlMock = $this->getMock('Zend\\Db\\Sql\\Sql', array('select'), array($this->mockAdapter)); $sqlMock->expects($this->any())->method('select')->will($this->returnValue($selectMock)); $this->setRowGatewayState(array('sql' => $sqlMock)); // original mock returning updated data $this->mockResult->expects($this->any())->method('current')->will($this->returnValue(array('id' => 7, 'name' => 'fooUpdated'))); // populate forces an update in save(), seeds with original data (from db) $this->rowGateway->populate(array('id' => 6, 'name' => 'foo'), true); $this->rowGateway->id = 7; $this->rowGateway->save(); $this->assertEquals(array('id' => 7, 'name' => 'fooUpdated'), $this->rowGateway->toArray()); }
public function save() { $this->initialize(); $currentUserId = null; if (Auth::loggedIn()) { $currentUser = Auth::getUserInfo(); $currentUserId = intval($currentUser['id']); } /** * ACL Enforcement * Note: Field Write Blacklists are enforced at the object setter level * (AARG#__set, AARG#populate, AARG#offsetSet) */ if (!$this->rowExistsInDatabase()) { /** * Enforce Privilege: Table Add */ if (!$this->acl->hasTablePrivilege($this->table, 'add')) { $aclErrorPrefix = $this->acl->getErrorMessagePrefix(); throw new UnauthorizedTableAddException($aclErrorPrefix . "Table add access forbidden on table " . $this->table); } } else { $cmsOwnerId = $this->acl->getRecordCmsOwnerId($this, $this->table); /** * Enforce Privilege: "Little" Edit (I am the record CMS owner) */ if ($cmsOwnerId === intval($currentUserId)) { if (!$this->acl->hasTablePrivilege($this->table, 'edit')) { $recordPk = self::stringifyPrimaryKeyForRecordDebugRepresentation($this->primaryKeyData); $aclErrorPrefix = $this->acl->getErrorMessagePrefix(); throw new UnauthorizedTableEditException($aclErrorPrefix . "Table edit access forbidden on `" . $this->table . "` table record with {$recordPk} owned by the authenticated CMS user (#{$cmsOwnerId})."); } } else { if (!$this->acl->hasTablePrivilege($this->table, 'bigedit')) { $recordPk = self::stringifyPrimaryKeyForRecordDebugRepresentation($this->primaryKeyData); $recordOwner = false === $cmsOwnerId ? "no magic owner column" : "the CMS owner #{$cmsOwnerId}"; $aclErrorPrefix = $this->acl->getErrorMessagePrefix(); throw new UnauthorizedTableBigEditException($aclErrorPrefix . "Table bigedit access forbidden on `" . $this->table . "` table record with {$recordPk} and {$recordOwner}."); } } } try { return parent::save(); } catch (InvalidQueryException $e) { $this->logger()->fatal("Error running save on this data: " . print_r($this->data, true)); throw $e; } }
public function save() { parent::save(); }
public function testSave() { // If we insert a new row, we should be able to read the ID after saving it. // For the purposes of this test, the mocks are set up to always generate an // id of "example" (see setup method above). $row = new RowGateway('id', 'fake', $this->mockAdapter); $row->foo = 'bar'; $row->save(); $this->assertEquals('example', $row->id); $this->assertEquals('example', $row['id']); }
/** * Set attribute value * * @param RowGateway $entityRow entity object * @param RowGateway $attribute attribute object * @param mixed $value attribute value */ public function setAttributeValue($entityRow, $attribute, $value) { if (is_string($attribute)) { $attribute = $this->getAttribute($attribute); } $typeTable = $this->getTypeTable($attribute); $valueRow = $this->getValueRow($entityRow, $attribute); if (!$valueRow) { $valueRow = new RowGateway('id', $typeTable->getTable(), $typeTable->getAdapter()); $valueRow->attribute_id = $this->getAttributeId($attribute); $valueRow->entity_id = $this->getEntityId($entityRow); } $valueRow->value = $value; $valueRow->save(); }
/** * @return bool $successfulUpdate */ protected function update() : bool { $success = (bool) $this->rowGateway->save(); return $success; }
public function demoRowGatewayStandaloneAction() { // query the database $resultSet = $this->localAdapter->query('SELECT * FROM test WHERE id = ?', array(2)); // get array of data $rowData = $resultSet->current()->getArrayCopy(); // row gateway $rowGateway = new RowGateway('id', 'test', $this->localAdapter); $rowGateway->populate($rowData, true); $rowGateway->title = 'New title - RowGateway'; $rowGateway->save(); // or delete this row: // $rowGateway->delete(); }