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

Call to a member function __toString() on string on custom type #11700

Open
NickOver opened this issue Oct 25, 2024 · 0 comments
Open

Call to a member function __toString() on string on custom type #11700

NickOver opened this issue Oct 25, 2024 · 0 comments

Comments

@NickOver
Copy link

Bug Report

Q A
Version 2.12.0

Summary

When using custom types, from some reason, when i use custom type as ID, its generate error.

Current behavior

Currently, its generate error Handling "App\Returns\Application\Command\CompleteReturnCommand" failed: Call to a member function __toString() on string

Expected behavior

I can read and update entities without modify convertToDatabaseValue method

How to reproduce

I have entity:

class CustomerReturn
{
    public function __construct(
        private ReturnId $id,
        private OrderId $returningOrderId,
    ) {}
}

With mapping:

<entity name="App\Returns\Domain\CustomerReturn" table="customer_return" >
        <id name="id" type="ReturnId" column="id" />
        <field name="returningOrderId" type="OrderId" column="returning_order_id" length="80" />
</entity>

Types:

class OrderIdType extends Type
{
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
    {
        return 'VARCHAR(36)';
    }

    public function convertToPHPValue($value, AbstractPlatform $platform): OrderId
    {
        return new OrderId($value);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform): string
    {
        return $value->__toString();
    }
}
class ReturnIdType extends Type
{
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
    {
        return 'VARCHAR(36)';
    }

    public function convertToPHPValue($value, AbstractPlatform $platform): ReturnId
    {
        return new ReturnId($value);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform): string
    {
        return $value->__toString();
    }
}

Objects:

class OrderId
{
    public function __construct(
        private string $id,
    ) {}

    public static function fromString(string $id): OrderId
    {
        return new OrderId($id);
    }

    public function getId(): string
    {
        return $this->id;
    }

    public function __toString(): string
    {
        return $this->id;
    }
}
class ReturnId
{
    public function __construct(
        private string $id,
    ) {}

    public static function fromString(string $id): ReturnId
    {
        return new ReturnId($id);
    }

    public function getId(): string
    {
        return $this->id;
    }

    public function __toString(): string
    {
        return $this->id;
    }
}

From unknown reason, i got error mentioned above. When i replace method in ReturnIdType to:

    public function convertToDatabaseValue($value, AbstractPlatform $platform): string
    {
        if (is_string($value))
            return $value;
        return $value->__toString();
    }

Everything works fine.
Only difference is one type i use as id field, and second as "normal" field.

@ostrolucky ostrolucky transferred this issue from doctrine/DoctrineBundle Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant