Skip to content

Cannot access uninitialized non-nullable property by reference #52907

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

Closed
talhaamir4130 opened this issue Dec 6, 2023 · 5 comments
Closed

Cannot access uninitialized non-nullable property by reference #52907

talhaamir4130 opened this issue Dec 6, 2023 · 5 comments

Comments

@talhaamir4130
Copy link

Symfony version(s) affected

6.3.10

Description

My previous Symfony version was 6.3.8 , But when I run composer update to update all packages with symfony versions I am getting this error: An exception has been thrown during the rendering of a template ("Cannot access uninitialized non-nullable property ArticleCombine::$articlePriceOptions by reference").

How to reproduce

Entity : ArticleCombine.php

#[ORM\Entity(ArticleCombineRepository::class)]
class ArticleCombine
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    /** @var Collection<int, ArticlePrice> */
    #[ORM\OneToMany(targetEntity: ArticlePrice::class, mappedBy: 'articleCombine', cascade: ['persist'])]
    #[ORM\OrderBy(['type' => 'ASC', 'qty' => 'ASC', 'id' => 'ASC'])]
    private Collection $articlePriceOptions;

    public function __construct()
    {
        $this->articlePriceOptions = new ArrayCollection();
    }

    public function getArticlePriceConsumerOptions(): mixed
    {
        return $this->articlePriceOptions->filter(
            function (ArticlePrice $articlePrice) {
                return 'c' == $articlePrice->getType();
            }
        );
    }
}

Entity: ArticlePrice.php

#[ORM\Entity(ArticlePriceRepository::class)]
#[Index(name: 'priceType', columns: ['type', 'articleCombineId'])]
class ArticlePrice
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: ArticleCombine::class, inversedBy: 'articlePriceOptions', cascade: ['persist'])]
    #[ORM\JoinColumn(name: 'articleCombineId', referencedColumnName: 'id', nullable: false)]
    private ?ArticleCombine $articleCombine = null;
}

Custom Twig Extension:

class ShopTwigExtension extends AbstractExtension implements GlobalsInterface
{
    public function getFilters(): array
    {
        return [
            new TwigFilter('calculatePriceScale', $this->calculatePriceScale(...)),
        ];
    }

    public function calculatePriceScale(ArticleCombine $combine, Article $article, array $optionArray, int $qty, ?User $user): array
    {
        $result = $combine->getArticlePriceConsumerOptions(); // Exception thrown here
    }
}

Calling the function in twig :

{% set priceConfig = combine.articleCombine|calculatePriceScale(combine.articleLanguage.article, option, qty, user) %}

Possible Solution

No response

Additional Context

If I dont use twig extension, and do something like this in a Controller:

/** @var ArticleCombine */
$articleCombine = $entityManager->getRepository(ArticleCombine::class)->findOneBy([]);
dd($articleCombine->getArticlePriceConsumerOptions());

This works totally fine. It seems Its only happeing when getthing this value through twig extension.

@xabbuh
Copy link
Member

xabbuh commented Dec 6, 2023

This looks a lot like #52753. Can you please try if the issue resolves itself when you clear your application's cache?

@talhaamir4130
Copy link
Author

talhaamir4130 commented Dec 6, 2023

@xabbuh Clearing the cache doesn't work for me.

I tried :

  • running composer install
  • running php bin/console c:c
  • removing var directory

Everytime its the same thing.

Also the linked issue is my alt account.

@xabbuh
Copy link
Member

xabbuh commented Dec 6, 2023

Can you then please create a small example application that allows to reproduce your issue?

@talhaamir4130
Copy link
Author

Sure, I can try. 👀

@xabbuh
Copy link
Member

xabbuh commented Dec 16, 2023

I am going to close here for now. Please comment when you have something that allows to reproduce and we can consider to reopen.

@xabbuh xabbuh closed this as not planned Won't fix, can't repro, duplicate, stale Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants