Skip to content

Commit 0401335

Browse files
committed
Save relationships when create and update record
1 parent f0be38a commit 0401335

File tree

3 files changed

+63
-62
lines changed

3 files changed

+63
-62
lines changed

src/Concerns/CrudOperationsTrait.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Relaticle\Flowforge\Concerns;
66

7+
use Filament\Forms\Form;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Eloquent\SoftDeletingScope;
910

@@ -15,15 +16,19 @@ trait CrudOperationsTrait
1516
/**
1617
* Create a new record with the given attributes.
1718
*
18-
* @param array<string, mixed> $attributes The record attributes
19+
* @param Form $form
20+
* @param mixed $currentColumn
21+
* @return Model|null
1922
*/
20-
public function createRecord(array $attributes, mixed $currentColumn): ?Model
23+
public function createRecord(Form $form, mixed $currentColumn): ?Model
2124
{
2225
$model = $this->baseQuery->getModel()->newInstance();
2326

24-
$model->fill($attributes);
27+
$model->fill($form->getState());
2528

2629
if ($model->save()) {
30+
$form->model($model)->saveRelationships();
31+
2732
return $model;
2833
}
2934

@@ -33,20 +38,23 @@ public function createRecord(array $attributes, mixed $currentColumn): ?Model
3338
/**
3439
* Update an existing record with the given attributes.
3540
*
36-
* @param Model $record The record to update
37-
* @param array<string, mixed> $attributes The record attributes to update
41+
* @param Model $record The record to update
42+
* @param Form $form
43+
* @return bool
3844
*/
39-
public function updateRecord(Model $record, array $attributes): bool
45+
public function updateRecord(Model $record, Form $form): bool
4046
{
41-
$record->fill($attributes);
47+
$record->fill($form->getState());
48+
49+
$form->model($record)->saveRelationships();
4250

4351
return $record->save();
4452
}
4553

4654
/**
4755
* Delete an existing record.
4856
*
49-
* @param Model $record The record to delete
57+
* @param Model $record The record to delete
5058
*/
5159
public function deleteRecord(Model $record): bool
5260
{
@@ -56,11 +64,11 @@ public function deleteRecord(Model $record): bool
5664
/**
5765
* Update the order of records in a column.
5866
*
59-
* @param string|int $columnId The column ID
60-
* @param array<int, mixed> $recordIds The record IDs in their new order
67+
* @param string|int $columnId The column ID
68+
* @param array<int, mixed> $recordIds The record IDs in their new order
6169
* @return bool Whether the operation was successful
6270
*/
63-
public function updateRecordsOrderAndColumn(string | int $columnId, array $recordIds): bool
71+
public function updateRecordsOrderAndColumn(string|int $columnId, array $recordIds): bool
6472
{
6573
$orderField = $this->config->getOrderField();
6674
$columnField = $this->config->getColumnField();

src/Contracts/KanbanAdapterInterface.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,20 @@ public function getEditForm(Form $form): Form;
6767
/**
6868
* Create a new record with the given attributes.
6969
*
70-
* @param array<string, mixed> $attributes The record attributes
70+
* @param Form $form
71+
* @param mixed $currentColumn
72+
* @return Model|null
7173
*/
72-
public function createRecord(array $attributes, mixed $currentColumn): ?Model;
74+
public function createRecord(Form $form, mixed $currentColumn): ?Model;
7375

7476
/**
7577
* Update an existing record with the given attributes.
7678
*
77-
* @param Model $record The record to update
78-
* @param array<string, mixed> $attributes The record attributes to update
79+
* @param Model $record The record to update
80+
* @param Form $form
81+
* @return bool
7982
*/
80-
public function updateRecord(Model $record, array $attributes): bool;
83+
public function updateRecord(Model $record, Form $form): bool;
8184

8285
/**
8386
* Delete an existing record.

src/Livewire/KanbanBoard.php

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class KanbanBoard extends Component implements HasForms
6767
/**
6868
* The active card for modal operations.
6969
*/
70-
public string | int | null $currentRecord = null;
70+
public string|int|null $currentRecord = null;
7171

7272
/**
7373
* Search query for filtering cards.
@@ -103,17 +103,18 @@ class KanbanBoard extends Component implements HasForms
103103
/**
104104
* Initialize the Kanban board.
105105
*
106-
* @param KanbanAdapterInterface $adapter The Kanban adapter
107-
* @param int|null $initialCardsCount The initial number of cards to load per column
108-
* @param int|null $cardsIncrement The number of cards to load on "load more"
109-
* @param array<int, string> $searchable The searchable fields
106+
* @param KanbanAdapterInterface $adapter The Kanban adapter
107+
* @param int|null $initialCardsCount The initial number of cards to load per column
108+
* @param int|null $cardsIncrement The number of cards to load on "load more"
109+
* @param array<int, string> $searchable The searchable fields
110110
*/
111111
public function mount(
112112
KanbanAdapterInterface $adapter,
113-
?int $initialCardsCount = null,
114-
?int $cardsIncrement = null,
115-
array $searchable = []
116-
): void {
113+
?int $initialCardsCount = null,
114+
?int $cardsIncrement = null,
115+
array $searchable = []
116+
): void
117+
{
117118
$this->adapter = $adapter;
118119
$this->searchable = $searchable;
119120
$this->config = $this->adapter->getConfig();
@@ -130,7 +131,7 @@ public function mount(
130131

131132
// Initialize columns
132133
$this->columns = collect($this->config->getColumnValues())
133-
->map(fn ($label, $value) => [
134+
->map(fn($label, $value) => [
134135
'id' => $value,
135136
'label' => $label,
136137
'color' => $this->resolveColumnColors()[$value] ?? null,
@@ -250,30 +251,30 @@ protected function loadColumnsData(): void
250251
/**
251252
* Get items for a specific column.
252253
*
253-
* @param string|int $columnId The column ID
254+
* @param string|int $columnId The column ID
254255
* @return array The formatted items
255256
*/
256-
public function getItemsForColumn(string | int $columnId): array
257+
public function getItemsForColumn(string|int $columnId): array
257258
{
258259
return $this->columnCards[$columnId] ?? [];
259260
}
260261

261262
/**
262263
* Get the total count of items for a specific column.
263264
*
264-
* @param string|int $columnId The column ID
265+
* @param string|int $columnId The column ID
265266
* @return int The total count
266267
*/
267-
public function getColumnItemsCount(string | int $columnId): int
268+
public function getColumnItemsCount(string|int $columnId): int
268269
{
269270
return $this->adapter->getColumnItemsCount($columnId);
270271
}
271272

272273
/**
273274
* Load more items for a column.
274275
*
275-
* @param string $columnId The column ID
276-
* @param int|null $count The number of items to load
276+
* @param string $columnId The column ID
277+
* @param int|null $count The number of items to load
277278
*/
278279
public function loadMoreItems(string $columnId, ?int $count = null): void
279280
{
@@ -291,7 +292,7 @@ public function loadMoreItems(string $columnId, ?int $count = null): void
291292
/**
292293
* Format items for display.
293294
*
294-
* @param Collection $items The items to format
295+
* @param Collection $items The items to format
295296
* @return array The formatted items
296297
*/
297298
protected function formatItems(Collection $items): array
@@ -302,8 +303,8 @@ protected function formatItems(Collection $items): array
302303
/**
303304
* Update the order of cards in a column.
304305
*
305-
* @param string|int $columnId The column ID
306-
* @param array $cardIds The card IDs in their new order
306+
* @param string|int $columnId The column ID
307+
* @param array $cardIds The card IDs in their new order
307308
* @return bool Whether the operation was successful
308309
*/
309310
public function updateRecordsOrderAndColumn($columnId, $cardIds): bool
@@ -320,7 +321,7 @@ public function updateRecordsOrderAndColumn($columnId, $cardIds): bool
320321
/**
321322
* Open the create form modal.
322323
*
323-
* @param string $columnId The column ID for the new card
324+
* @param string $columnId The column ID for the new card
324325
*/
325326
public function openCreateForm(string $columnId): void
326327
{
@@ -338,17 +339,17 @@ public function openCreateForm(string $columnId): void
338339
/**
339340
* Open the edit form modal.
340341
*
341-
* @param string|int $recordId The card ID to edit
342-
* @param string|int $columnId The column ID containing the card
342+
* @param string|int $recordId The card ID to edit
343+
* @param string|int $columnId The column ID containing the card
343344
*/
344-
public function openEditForm(string | int $recordId, string | int $columnId): void
345+
public function openEditForm(string|int $recordId, string|int $columnId): void
345346
{
346347
$this->currentColumn = $columnId;
347348
$this->currentRecord = $recordId;
348349

349350
$record = $this->adapter->getModelById($recordId);
350351

351-
if (! $record) {
352+
if (!$record) {
352353
Notification::make()
353354
->title('Card not found')
354355
->danger()
@@ -376,7 +377,7 @@ public function openEditForm(string | int $recordId, string | int $columnId): vo
376377
*/
377378
public function createRecord(): void
378379
{
379-
if (! $this->permissions['canCreate']) {
380+
if (!$this->permissions['canCreate']) {
380381
Notification::make()
381382
->title('You do not have permission to create records')
382383
->danger()
@@ -385,23 +386,14 @@ public function createRecord(): void
385386
return;
386387
}
387388

388-
// Use form state to get data with validation applied
389-
$data = $this->createRecordForm->getState();
390-
391-
// Ensure column field is set
392-
$columnField = $this->config->getColumnField();
393-
if (! isset($data[$columnField])) {
394-
$data[$columnField] = $this->currentColumn;
395-
}
396-
397-
$card = $this->adapter->createRecord($data, $this->currentColumn);
389+
$record = $this->adapter->createRecord($this->createRecordForm, $this->currentColumn);
398390

399-
if ($card) {
391+
if ($record) {
400392
$this->refreshBoard();
401393
$this->resetCreateForm();
402394

403395
$this->dispatch('kanban-record-created', [
404-
'record' => $card,
396+
'record' => $record,
405397
]);
406398

407399
Notification::make()
@@ -433,11 +425,9 @@ private function resetCreateForm(): void
433425
*/
434426
public function updateRecord(): void
435427
{
436-
// Use form state to get data with any relationship handling applied
437-
$data = $this->editRecordForm->getState();
438428
$record = $this->adapter->getModelById($this->currentRecord);
439429

440-
if (! $record) {
430+
if (!$record) {
441431
Notification::make()
442432
->title(__(':Record not found', ['record' => $this->config->getSingularCardLabel()]))
443433
->danger()
@@ -446,7 +436,7 @@ public function updateRecord(): void
446436
return;
447437
}
448438

449-
$success = $this->adapter->updateRecord($record, $data);
439+
$success = $this->adapter->updateRecord($record, $this->editRecordForm);
450440

451441
if ($success) {
452442
$this->refreshBoard();
@@ -483,10 +473,10 @@ public function resetEditForm(): void
483473
/**
484474
* Open the delete confirmation modal.
485475
*
486-
* @param string|int $cardId The card ID to delete
487-
* @param string $columnId The column ID containing the card
476+
* @param string|int $cardId The card ID to delete
477+
* @param string $columnId The column ID containing the card
488478
*/
489-
public function confirmDelete(string | int $cardId, string $columnId): void
479+
public function confirmDelete(string|int $cardId, string $columnId): void
490480
{
491481
$this->currentRecord = $cardId;
492482
$this->currentColumn = $columnId;
@@ -497,7 +487,7 @@ public function confirmDelete(string | int $cardId, string $columnId): void
497487
*/
498488
public function deleteRecord(): void
499489
{
500-
if (! $this->permissions['canDelete']) {
490+
if (!$this->permissions['canDelete']) {
501491
Notification::make()
502492
->title(__('You do not have permission to delete :records', ['records' => $this->config->getPluralCardLabel()]))
503493
->danger()
@@ -508,7 +498,7 @@ public function deleteRecord(): void
508498

509499
$record = $this->adapter->getModelById($this->currentRecord);
510500

511-
if (! $record) {
501+
if (!$record) {
512502
Notification::make()
513503
->title('Record not found')
514504
->danger()

0 commit comments

Comments
 (0)