Skip to content

Cannot access uninitialized non-nullable property by reference #52907

Closed as not planned
@talhaamir4130

Description

@talhaamir4130

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions