doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.92k stars 2.51k forks source link

Incorrect type in tutorial #11492

Closed SamMousa closed 3 months ago

SamMousa commented 3 months ago

Just getting into Doctrine ORM by reading the docs from A to Z. Noticed that the parameter type in the example is wrong making the example non-functional.

https://github.com/doctrine/orm/blob/9d4f54b9a476f13479c3845350b12c466873fc42/docs/en/tutorials/composite-primary-keys.rst?plain=1#L150

<?php
namespace Application\Model;

use Doctrine\Common\Collections\ArrayCollection;

#[Entity]
class Article
{
    #[Id, Column, GeneratedValue]
    private int|null $id = null;
    #[Column]
    private string $title;

    /** @var ArrayCollection<string, ArticleAttribute> */
    #[OneToMany(targetEntity: ArticleAttribute::class, mappedBy: 'article', cascade: ['ALL'], indexBy: 'attribute')]
    private Collection $attributes;

    public function addAttribute(string $name, ArticleAttribute $value): void
    {
        $this->attributes[$name] = new ArticleAttribute($name, $value, $this);
    }
}

#[Entity]
class ArticleAttribute
{
    #[Id, ManyToOne(targetEntity: Article::class, inversedBy: 'attributes')]
    private Article $article;

    #[Id, Column]
    private string $attribute;

    #[Column]
    private string $value;

    public function __construct(string $name, string $value, Article $article)
    {
        $this->attribute = $name;
        $this->value = $value;
        $this->article = $article;
    }
}
greg0ire commented 3 months ago

Indeed. Can you please send a pull request against 2.19.x? We will merge it up afterwards.

SamMousa commented 3 months ago

A more structural / robust approach would be to automatically type check all code samples in CI using something like PHPStan.

greg0ire commented 3 months ago

True. I don't know how to do that however.