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

Incorrect type in tutorial #11492

Closed
SamMousa opened this issue Jun 11, 2024 · 3 comments
Closed

Incorrect type in tutorial #11492

SamMousa opened this issue Jun 11, 2024 · 3 comments

Comments

@SamMousa
Copy link
Contributor

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.

$this->attributes[$name] = new ArticleAttribute($name, $value, $this);

<?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
Copy link
Member

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

@SamMousa
Copy link
Contributor Author

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

@greg0ire
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants