Andrew Wallo 6 个月前
父节点
当前提交
58357001a8
共有 1 个文件被更改,包括 41 次插入15 次删除
  1. 41
    15
      app/Casts/TransactionAmountCast.php

+ 41
- 15
app/Casts/TransactionAmountCast.php 查看文件

11
 
11
 
12
 class TransactionAmountCast implements CastsAttributes
12
 class TransactionAmountCast implements CastsAttributes
13
 {
13
 {
14
-    private array $currencyCache = [];
14
+    /**
15
+     * Static cache to persist across instances
16
+     */
17
+    private static array $currencyCache = [];
18
+
19
+    /**
20
+     * Eagerly load all required bank accounts at once if needed
21
+     */
22
+    private function loadMissingBankAccounts(array $ids): void
23
+    {
24
+        $missingIds = array_filter($ids, static fn ($id) => ! isset(self::$currencyCache[$id]) && $id !== null);
25
+
26
+        if (empty($missingIds)) {
27
+            return;
28
+        }
29
+
30
+        /** @var BankAccount[] $accounts */
31
+        $accounts = BankAccount::with('account')
32
+            ->whereIn('id', $missingIds)
33
+            ->get();
34
+
35
+        foreach ($accounts as $account) {
36
+            self::$currencyCache[$account->id] = $account->account->currency_code ?? CurrencyAccessor::getDefaultCurrency();
37
+        }
38
+    }
15
 
39
 
16
     public function get(Model $model, string $key, mixed $value, array $attributes): string
40
     public function get(Model $model, string $key, mixed $value, array $attributes): string
17
     {
41
     {
18
         // Attempt to retrieve the currency code from the related bankAccount->account model
42
         // Attempt to retrieve the currency code from the related bankAccount->account model
19
-        $currencyCode = $this->getCurrencyCodeFromBankAccountId($attributes['bank_account_id'] ?? null);
43
+        $bankAccountId = $attributes['bank_account_id'] ?? null;
44
+
45
+        if ($bankAccountId !== null && ! isset(self::$currencyCache[$bankAccountId])) {
46
+            $this->loadMissingBankAccounts([$bankAccountId]);
47
+        }
48
+
49
+        $currencyCode = $this->getCurrencyCodeFromBankAccountId($bankAccountId);
20
 
50
 
21
         if ($value !== null) {
51
         if ($value !== null) {
22
             return CurrencyConverter::prepareForMutator($value, $currencyCode);
52
             return CurrencyConverter::prepareForMutator($value, $currencyCode);
30
      */
60
      */
31
     public function set(Model $model, string $key, mixed $value, array $attributes): int
61
     public function set(Model $model, string $key, mixed $value, array $attributes): int
32
     {
62
     {
33
-        $currencyCode = $this->getCurrencyCodeFromBankAccountId($attributes['bank_account_id'] ?? null);
63
+        $bankAccountId = $attributes['bank_account_id'] ?? null;
64
+
65
+        if ($bankAccountId !== null && ! isset(self::$currencyCache[$bankAccountId])) {
66
+            $this->loadMissingBankAccounts([$bankAccountId]);
67
+        }
68
+
69
+        $currencyCode = $this->getCurrencyCodeFromBankAccountId($bankAccountId);
34
 
70
 
35
         if (is_numeric($value)) {
71
         if (is_numeric($value)) {
36
             $value = (string) $value;
72
             $value = (string) $value;
42
     }
78
     }
43
 
79
 
44
     /**
80
     /**
45
-     * Using this is necessary because the relationship is not always loaded into memory when the cast is called
46
-     * Instead of using: $model->bankAccount->account->currency_code directly, find the bank account and get the currency code
81
+     * Get currency code from the cache or use default
47
      */
82
      */
48
     private function getCurrencyCodeFromBankAccountId(?int $bankAccountId): string
83
     private function getCurrencyCodeFromBankAccountId(?int $bankAccountId): string
49
     {
84
     {
51
             return CurrencyAccessor::getDefaultCurrency();
86
             return CurrencyAccessor::getDefaultCurrency();
52
         }
87
         }
53
 
88
 
54
-        if (isset($this->currencyCache[$bankAccountId])) {
55
-            return $this->currencyCache[$bankAccountId];
56
-        }
57
-
58
-        $bankAccount = BankAccount::find($bankAccountId);
59
-
60
-        $currencyCode = $bankAccount?->account?->currency_code ?? CurrencyAccessor::getDefaultCurrency();
61
-        $this->currencyCache[$bankAccountId] = $currencyCode;
62
-
63
-        return $currencyCode;
89
+        return self::$currencyCache[$bankAccountId] ?? CurrencyAccessor::getDefaultCurrency();
64
     }
90
     }
65
 }
91
 }

正在加载...
取消
保存