DirectoryTree / LdapRecord

A fully-featured LDAP framework.
https://ldaprecord.com
MIT License
500 stars 44 forks source link

Attribute casting seem to work only in one direction #707

Closed lukasmu closed 4 months ago

lukasmu commented 5 months ago

It is possible to define the $casts property on models as explained here: https://ldaprecord.com/docs/core/v3/model-mutators/#attribute-casting

However, the casting operation seems to work only when getting attributes, not when setting attributes. This is different from how casting works in Laravel's Eloquent and thus confused me.

Not sure if it's easily possible to add this, otherwise I would recommend to update the documentation.

Example:

namespace App\Models\Ldap;

use LdapRecord\Models\ActiveDirectory\User as BaseUser;

class User extends BaseUser
{
    protected array $casts = [
        'msExchHideFromAddressList' => 'boolean',
    ];
}

What works:

$user = User::find('cn=John Doe,dc=local,dc=com');
assert(is_bool($user->msExchHideFromAddressList));

What doesn't work:

$user = User::find('cn=John Doe,dc=local,dc=com');
$user->msExchHideFromAddressList = false;
$user->save();
stevebauman commented 4 months ago

Thanks for the report @lukasmu! Apologies for the delayed response on this issue. I'd classify this as a bug -- it should definitely occur both ways. I think this is the case with some casts and not others. Let me update this and make sure its consistent across the board.

stevebauman commented 4 months ago

This has been now resolved in v3.6.2. Run composer update and you're all set. Thanks!