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

You problably dont want to inject a "<class>" here #2404

Open
tjveldhuizen opened this issue Nov 4, 2024 · 0 comments
Open

You problably dont want to inject a "<class>" here #2404

tjveldhuizen opened this issue Nov 4, 2024 · 0 comments

Comments

@tjveldhuizen
Copy link

I am using the Symfony forms, based on a DTO. So I have a EditUserDto class which is bound to my form and a User Doctrine entity class. Both classes are excluded for dependency injection.

The Symfony plugin (pro) gives the warning 'You probably dont want to inject a "User" here' (by the way, the ' is missing in "don't") when I use the entity in the constructor of the DTO. Is it supposed to do so? What's a better practice to set the initial values for my DTO?

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\DependencyInjection\Attribute\Exclude;

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

    #[ORM\Column]
    protected ?string $name = null;

    public function getName(): string
    {
        return $this->name;
    }
}

#[Exclude]
class EditUserDto {
    public ?string $username;

    public function __construct(User $user) {
        $this->username = $user->getName();
    }
}
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

1 participant