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

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

Open
gremo opened this issue Mar 7, 2024 · 15 comments
Labels
LiveComponent Status: Waiting Feedback Needs feedback from the author

Comments

@gremo
Copy link
Contributor

gremo commented Mar 7, 2024

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.

@smnandre
Copy link
Member

smnandre commented Mar 7, 2024

Could you show us how you call this method from your template ?

@gremo
Copy link
Contributor Author

gremo commented Mar 7, 2024

@smnandre thanks for helping, I'm not even printing the product in my template. The error is raised from a live listener:

    #[LiveProp]
    public ?Product $product = null;

    #[LiveListener("barcode_scanned")]
    public function onScan(#[LiveArg()] string $value): void
    {
        $envelope = $this->messageBus->dispatch(
            new FetchProductMessage($value, $this->order->customer, $this->profile)
        );
        $product = $envelope->last(HandledStamp::class)->getResult();

        $this->product = $product;
    }

@smnandre
Copy link
Member

smnandre commented Mar 7, 2024

You may need to show more code, because i see nowhere where is the $discountedUnitPrice call mentionned in your error.

At least you should give a bit more details concerning this exception, where it is raised, etc.

@gremo
Copy link
Contributor Author

gremo commented Mar 7, 2024

Here is a screenshot, if it may help. I'm not using product in my template, in fact the error is related to the hydration process.

image

@smnandre
Copy link
Member

smnandre commented Mar 7, 2024

So we can close this issue, as it's replaced by #1590 right ?

@gremo
Copy link
Contributor Author

gremo commented Mar 7, 2024

There are two different issues, just naming are similar, a bit confusing Indeed...

@gremo
Copy link
Contributor Author

gremo commented Mar 7, 2024

@smnandre to be clear: I get that error for any "virtual" property of my DTO.

It may be related to the hydration problem explained here, because Symfony doesn't know how to set back the value when the hydrate occurs (and indeed it looks for a property named discountedUnitPrice, which doesn't exists).

It would be a nice idea to allow to exclude those "virtual properties". What If I only need getDiscountedUnitPrice on the server and not on the client?

For example "Do not try to dehydrate/hydrate this property":

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

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@gremo
Copy link
Contributor Author

gremo commented Sep 9, 2024

I think isn't resolved yet.

@carsonbot carsonbot removed the Stalled label Sep 9, 2024
@smnandre
Copy link
Member

smnandre commented Sep 9, 2024

Could you please create a small reproducer (the minimal app possible to reproduce / observe / investigate the bug) ?

It really is the best method, so we can pull some repository and immediately work on it :)

@smnandre smnandre added Status: Waiting Feedback Needs feedback from the author LiveComponent labels Sep 9, 2024
@gremo
Copy link
Contributor Author

gremo commented Sep 11, 2024

Yes @smnandre it's ok to provide a container for running the example or you do by your own?

@smnandre
Copy link
Member

A small repository with minimal install would be ideal :)

See

@gremo
Copy link
Contributor Author

gremo commented Oct 6, 2024

Repo: https://github.com/gremo/github-issue-1589

  • User 1 has a virtual method that starts with "get" and live hydrator get confused, I'm not even using that virtual method!
  • User 2 has a virtual method but it doesn't start with "get" and all works fine

Immagine 2024-10-06 135950

@smnandre
Copy link
Member

smnandre commented Oct 6, 2024

If you have time to suggest a fix we'll look at it, but we're reworking this part currently and this is such a weird case... did you try with custom hydration ?

@bastienrossi
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LiveComponent Status: Waiting Feedback Needs feedback from the author
Projects
None yet
Development

No branches or pull requests

4 participants