/** * @test */ public function updateForTwoChangedRecordsReturnsTwo() { $uid1 = $this->testingFramework->createRecord('tx_oelib_test'); $uid2 = $this->testingFramework->createRecord('tx_oelib_test'); self::assertSame(2, Tx_Oelib_Db::update('tx_oelib_test', 'uid IN(' . $uid1 . ',' . $uid2 . ')', array('title' => 'foo'))); }
/** * Writes a model to the database. Does nothing if database access is * denied, if the model is clean, if the model has status dead, virgin or * ghost, if the model is read-only or if there is no data to set. * * @param Tx_Oelib_Model $model the model to write to the database * * @return void */ public function save(Tx_Oelib_Model $model) { if ($this->isModelAMemoryOnlyDummy($model)) { throw new InvalidArgumentException('This model is a memory-only dummy that must not be saved.', 1331319682); } if (!$this->hasDatabaseAccess() || !$model->isDirty() || !$model->isLoaded() || $model->isReadOnly()) { return; } $data = $this->getPreparedModelData($model); $this->cacheModelByKeys($model, $data); if ($model->hasUid()) { Tx_Oelib_Db::update($this->getTableName(), 'uid = ' . $model->getUid(), $data); $this->deleteManyToManyRelationIntermediateRecords($model); } else { $this->prepareDataForNewRecord($data); $model->setUid(Tx_Oelib_Db::insert($this->getTableName(), $data)); $this->map->add($model); } if ($model->isDeleted()) { $model->markAsDead(); } else { $model->markAsClean(); // We save the 1:n relations after marking this model as clean // in order to avoid infinite loops when the foreign model tries // to save this parent. $this->saveOneToManyRelationRecords($model); $this->createManyToManyRelationIntermediateRecords($model); } }
/** * Changes an existing dummy record and stores the new data for this * record. Only fields that get new values in $recordData will be changed, * everything else will stay untouched. * * The array with the new recordData must contain at least one entry, but * must not contain a new UID for the record. If you need to change the UID, * you have to create a new record! * * @param string $table the name of the table, must not be empty * @param int $uid the UID of the record to change, must not be empty * @param array $recordData * associative array containing key => value pairs for those fields of the record that need to be changed, * must not be empty * * @return void * * @throws InvalidArgumentException * @throws BadMethodCallException */ public function changeRecord($table, $uid, $recordData) { $dummyColumnName = $this->getDummyColumnName($table); if (!$this->isTableNameAllowed($table)) { throw new InvalidArgumentException('The table "' . $table . '" is not on the lists with allowed tables.', 1331489997); } if ($uid === 0) { throw new InvalidArgumentException('The parameter $uid must not be zero.', 1331490003); } if (empty($recordData)) { throw new InvalidArgumentException('The array with the new record data must not be empty.', 1331490008); } if (isset($recordData['uid'])) { throw new InvalidArgumentException('The parameter $recordData must not contain changes to the UID of a record.', 1331490017); } if (isset($recordData[$dummyColumnName])) { throw new InvalidArgumentException('The parameter $recordData must not contain changes to the field "' . $dummyColumnName . '". It is impossible to convert a dummy record into a regular record.', 1331490024); } if (!$this->countRecords($table, 'uid=' . $uid)) { throw new BadMethodCallException('There is no record with UID ' . $uid . ' on table "' . $table . '".', 1331490033); } Tx_Oelib_Db::update($table, 'uid = ' . $uid . ' AND ' . $dummyColumnName . ' = 1', $recordData); }