Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom fields on cache #2091

Merged
merged 1 commit into from
May 16, 2022

Conversation

erikn69
Copy link
Contributor

@erikn69 erikn69 commented May 6, 2022

Not breaking change
Closes #1836
Closes #2046
Closes #2016

Set all models fields on cache, still using alias and arrays for smaller cache, added automatic alias for avoid using i,n,g,
i,n,g causes a problem when you add a custom field like type, description, label

Before:

->select($permissionKey, "$permissionKey as i", 'name as n', 'guard_name as g')
->with("roles:$roleKey,$roleKey as i,name as n,guard_name as g")->get()
->map(function ($permission) {
return $permission->only('i', 'n', 'g') + $this->getSerializedRoleRelation($permission);

$this->cachedRoles[$item['i']] = $roleInstance->newFromBuilder([
$roleInstance->getKeyName() => $item['i'],
'name' => $item['n'],
'guard_name' => $item['g'],
]);

After: Less code, easier to understand

$permissions = $this->getPermissionClass()->select()->with('roles')->get()
///
$role = $roleInstance->newFromBuilder($this->aliasedArray($item));
$this->cachedRoles[$role->getKey()] = $role;

Results:

array:358 [▼
  0 => array:8 [▼
    "i" => 197
    "n" => "view_audit"
    "g" => "web"
    "l" => "Audit"
    "e" => null
    "y" => "A"
    "r" => array:3 [▼
      0 => array:5 [▼
        "i" => 0
        "t" => null
        "n" => "System"
        "g" => "web"
        "y" => "A"
      ]

1631207353a:358:{i:0;a:8:{s:1:"i";i:197;s:1:"n";s:10:"view_audit";s:1:"g";s:3:"web";s:1:"l";s:10:"Audit";s:1:"e";N;s:1:"y";s:1:"A";s:1:"r";a:3:{i:0;a:5:{s:1:"i";i:0;s:1:"t";N;s:1:"n";s:8:"System";s:1:"g";s:3:"web";s:1:"y";s:1:"A";}i:

Added test it_can_use_custom_fields_from_cache, checked not over hydration

image

@erikn69 erikn69 force-pushed the custom_fields_cache_fix branch 3 times, most recently from e1d1524 to 7d43883 Compare May 6, 2022 21:12
@freekmurze freekmurze merged commit cb86fd8 into spatie:main May 16, 2022
@freekmurze
Copy link
Member

Thanks!

@erikn69 erikn69 deleted the custom_fields_cache_fix branch February 17, 2023 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants