ux: [LiveComponent] Property does not exist exception when a LiveProp object has a virtual method?

My object is set as a LiveProp inside a Live component. It’s a plain DTO with a method added, a virtual one, named getDiscountedUnitPrice:

<?php

namespace App\Model;

class Product
{
    use ModelFromArrayTrait;

    public ?string $id = null;
    public ?string $name = null;
    public ?bool $enabled = null;
    public ?bool $discountable = null;
    public ?DiscountInterface $discount = null;

    public function getDiscountedUnitPrice(): ?float
    {
        return $this->discount?->calculate($this->unitPrice) ?? $this->unitPrice;
    }
}

As soon as my DTO become a LiveProp, I get the following error:

Property App\Model\Product::$discountedUnitPrice does not exist

If you name the method discountedUnitPrice it works like a charm. I think this is a bug to be addressed.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 18 (16 by maintainers)

Most upvoted comments

Hi, One solution I’ve found to solve the problem on my side is to use the useSerializerForHydration option.

#[LiveProp(useSerializerForHydration: true)]
public ?Product $product = null;