corcel icon indicating copy to clipboard operation
corcel copied to clipboard

Custom aliases and casts for post meta not working

Open noplanman opened this issue 4 years ago • 1 comments

  • Corcel Version: 5.0
  • Framework Name & Version: Laravel 8
  • PHP Version: 8.0
  • Database Driver & Version: MariaDB 10.6

Description:

When defining the $aliases on a custom model, the meta fields aren't loaded correctly and the $casts seem to be ignored. ~~(On a side note, default post aliases don't seem to be inherited correctly, like title or slug etc. I need to define them all as well.)~~ Fixed by adding use Corcel\Concerns\Aliases; to custom model.

Setting the meta data keys in $appends works correctly, but the $aliases seem to be ignored.

Maybe I'm missing something here?

Steps To Reproduce:

  1. Create a custom model and extend Corcel\Model\Post
  2. Add a custom meta field called channel_failures and set it to a value of 10.
  3. Define a custom meta field in $appends and make an alias in $aliases, add field to $casts.
    protected $casts = [
        'failures' => 'int', // doesn't work
        'channel_failures' => 'int', // doesn't work
    ];
    
    protected $appends = [
        'failures', // doesn't work
        'channel_failures', // works
    ];
    
    protected static $aliases = [
        'failures' => 'channel_failures', // doesn't work
    ];    
    
  4. Try it and see that it doesn't work as expected:
    MyPostType::first()->failures; // null
    MyPostType::first()->channel_failures; // "10" <- not being cast to int
    

noplanman avatar Aug 01 '21 17:08 noplanman

After diving into the code, I found that meta field aliases need to be defined as an array:

protected static $aliases = [
    'failures' => ['meta' => 'channel_failures'], // works!
];    

Still no luck with the $casts yet

noplanman avatar Aug 01 '21 18:08 noplanman