Carlos 2V
Carlos 2V

Reputation: 175

Could not convert PHP value X to type uuid. Expected one of the following types: string. But X implement \Stringable

I'm getting a conversion error when I call Doctrine's flush function to save X entity.

Could not convert PHP value of type X to type uuid. Expected one of the following types: null, string, Symfony\\Component\\Uid\\AbstractUid

final class X extends MyUuid
{

}
abstract class MyUuid implements \Stringable
{
    // receive string uuid
    final public function __construct(protected string $value)
    {
        
    }

    public function __toString(): string // return uuid string
    {
        return $this->value;
    }
}

I don't understand why it says it accepts string, but my class implements the \Stringable interface

Upvotes: -1

Views: 46

Answers (1)

yceruto
yceruto

Reputation: 9585

Doctrine ORM has strict typing declared, so it's not automatically cast to a string in this case.

According to that error, you're probably using the uuid type for this mapping.

If you want to declare a custom ID class that doesn't extend from AbstractUid, you'll have to define a custom DBAL type as well and teach Doctrine how to convert that object into a string and viceversa.

See how to do that here:

Upvotes: 0

Related Questions