Skip to content

Commit 3cfbf32

Browse files
committed
improve compliance with laravel model by providing static create method
1 parent 602be30 commit 3cfbf32

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ public static function all($columns = [])
129129
return static::scan($columns);
130130
}
131131

132+
public static function create(array $fillables = [], array $options = []){
133+
$instance = new static($fillables);
134+
$instance->save($options);
135+
return $instance;
136+
}
137+
132138
/**
133139
* Save the model to the database.
134140
*

tests/Model/ModelTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,29 @@ public function it_can_save_new_instance()
431431
$user->save();
432432
}
433433

434+
/** @test */
435+
public function it_can_static_create_new_instance()
436+
{
437+
$params = [
438+
'TableName' => 'User',
439+
'Item' => [
440+
'partition' => [
441+
'S' => 'p'
442+
]
443+
],
444+
'ConditionExpression' => 'attribute_not_exists(#1)',
445+
'ExpressionAttributeNames' => [
446+
'#1' => 'partition'
447+
]
448+
];
449+
450+
$connection = $this->newConnectionMock();
451+
$connection->shouldReceive('putItem')->with($params);
452+
$this->setConnectionResolver($connection);
453+
454+
$user = UserA::create(['partition' => 'p']);
455+
}
456+
434457
/** @test */
435458
public function it_cannot_save_new_instance_without_required_key()
436459
{

0 commit comments

Comments
 (0)