Skip to content

Commit aea0d38

Browse files
committed
Merge branch 'v1.x' into v2.x
2 parents 864a04c + 0e7ffeb commit aea0d38

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

BaseMapper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,10 @@ public function fetchAll(Query $select = null): Set
266266
* Save model to database.
267267
*
268268
* @param BaseModel $record
269+
* @param bool $forceUpdate
269270
* @throws Exception
270271
*/
271-
public function save(BaseModel $record): void
272+
public function save(BaseModel $record, bool $forceUpdate = false): void
272273
{
273274
$recordArray = $this->getFieldsForSave($record);
274275
if (count($recordArray) == 0) {
@@ -278,7 +279,7 @@ public function save(BaseModel $record): void
278279
$primaryKey = static::PRIMARY_KEY;
279280
/** @var string|int|null $recordPrimaryKey */
280281
$recordPrimaryKey = $record->{$primaryKey} ?? null;
281-
if (!empty($recordPrimaryKey) && !empty($record->getOriginalModel())) {
282+
if (!empty($recordPrimaryKey) && (!empty($record->getOriginalModel()) || $forceUpdate)) {
282283
$update = $this->connection->update((string)static::TABLE_NAME, $recordArray)->where(
283284
$primaryKey . ' = ?',
284285
$recordPrimaryKey

BaseModel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ public function getChangeField(BaseModel $oldModel, string $field): string
120120
/**
121121
* Save the current model.
122122
*
123+
* @param bool $forceUpdate
123124
* @throws NotFoundException
124125
*/
125-
public function save(): void
126+
public function save(bool $forceUpdate = false): void
126127
{
127128
/** @var ?class-string<BaseMapper> $mapperName */
128129
$mapperName = static::MAPPER_NAME;
@@ -131,6 +132,6 @@ public function save(): void
131132
}
132133
$mapper = new $mapperName();
133134

134-
$mapper->save($this);
135+
$mapper->save($this, $forceUpdate);
135136
}
136137
}

FeastTests/JsonTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ public function testMarshal(): void
9393
$item->secondItem = $secondItem;
9494

9595
$item->thirdItems = new \Feast\Collection\CollectionList(
96-
'string', [
96+
'string', [
9797
'test' => 'theTest',
9898
'test2' => 'theTest2'
9999
], preValidated: true
100100
);
101101

102102
$item->thirdSet = new \Feast\Collection\Set(
103-
'string', [
103+
'string', [
104104
'theTest',
105105
'theTest2'
106106
], preValidated: true
@@ -133,7 +133,7 @@ public function testUnmarshalInvalidConstructor(): void
133133
public function testUnmarshalSkipConstructor(): void
134134
{
135135
$item = Json::unmarshal('{"last_name":"test"}', \Mocks\BadJsonItem::class, true);
136-
$this->assertEquals('test',$item->lastName);
136+
$this->assertEquals('test', $item->lastName);
137137
}
138138

139139
public function testUnmarshalWithObject(): void
@@ -162,8 +162,8 @@ public function testUnmarshal(): void
162162
$result->otherTimestamp->setTimezone('America/New_York');
163163
$this->assertEquals('20210415', $result->timestamp->getFormattedDate('Ymd'));
164164
$this->assertEquals('20210405', $result->otherTimestamp->getFormattedDate('Ymd'));
165-
$this->assertInstanceOf(\Mocks\SecondItem::class,$result->secondItem);
166-
$this->assertInstanceOf(stdClass::class,$result->aClass);
165+
$this->assertInstanceOf(\Mocks\SecondItem::class, $result->secondItem);
166+
$this->assertInstanceOf(stdClass::class, $result->aClass);
167167
$this->assertEquals('ItWorks', $result->aClass->test);
168168
}
169169

Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function marshal(object $object, ?int $propertyTypesFlag = null):
106106
public static function unmarshal(string $data, string|object $objectOrClass, bool $skipConstructor = false): object
107107
{
108108
if (is_string($objectOrClass)) {
109-
$object = self::getObjectFromClassString( $objectOrClass, $skipConstructor,);
109+
$object = self::getObjectFromClassString($objectOrClass, $skipConstructor);
110110
} else {
111111
$object = $objectOrClass;
112112
}

docs/models.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,14 @@ $models = $mapper->findAllByFields(['name' => 'Feast', 'status' => 'active']); /
9797

9898
#### Saving Records
9999

100-
To save a model, simply call either the `save` method on the mapper, passing in the model as an argument or for
100+
To save a model, call either the `save` method on the mapper, passing in the model as an argument or for
101101
convenience, simply call the `save()` method on the model. FEAST will automatically perform an insert if it is a new
102-
model, or an update (passing only the changed fields) if it is not a new model. When the save method is called, the
103-
primary key is updated on the model for inserts.
102+
model, or an update (passing only the changed fields) if it is was retrieved from the database.
103+
104+
You can force an update by passing in `true` to the `save` method as the only parameter on the model method, or as the
105+
second parameter on the mapper method.
106+
107+
When the save method is called, the primary key is updated on the model for inserts.
104108

105109
Example:
106110

@@ -116,6 +120,21 @@ $mapper = new \Mapper\TestMapper();
116120
$mapper->save($model); // Model saved indirectly after previous save, updated.
117121
```
118122

123+
Force update
124+
125+
```php
126+
<?php
127+
$model = new \Model\Test();
128+
$model->id = 1;
129+
$model->name = 'Feast';
130+
131+
$model->save(true); // Model saves directly, inserted.
132+
133+
$model->name = 'NotFeast';
134+
$mapper = new \Mapper\TestMapper();
135+
$mapper->save($model, true); // Model saved indirectly after previous save, updated.
136+
```
137+
119138
#### Deleting Records
120139

121140
FEAST has many methods to delete records from the database. The main methods to delete data are listed below.

0 commit comments

Comments
 (0)