Menu

← Back to doctrine.*

Error Identifier: doctrine.associationType

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Article
{
	/** @var string */
	#[ORM\ManyToOne(targetEntity: Author::class)]
	private $author;
}

#[ORM\Entity]
class Author
{
	#[ORM\Id]
	#[ORM\Column]
	private int $id;
}

Why is it reported? #

This error is reported by the phpstan-doctrine extension.

The property type does not match what Doctrine expects based on the association mapping. For a ManyToOne or OneToOne association, the property type should be the target entity type (or nullable if the association is not required). For OneToMany or ManyToMany associations, the property type should be Collection<int, TargetEntity>.

In the example above, $author is typed as string but the ManyToOne mapping expects it to be Author|null.

How to fix it #

Change the property type to match the association mapping:

 <?php declare(strict_types = 1);
 
 use Doctrine\ORM\Mapping as ORM;

 #[ORM\Entity]
 class Article
 {
-	/** @var string */
+	/** @var Author|null */
 	#[ORM\ManyToOne(targetEntity: Author::class)]
-	private $author;
+	private ?Author $author = null;
 }

How to ignore this error #

You can use the identifier doctrine.associationType to ignore this error using a comment:

// @phpstan-ignore doctrine.associationType
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: doctrine.associationType

Rules that report this error #

  • PHPStan\Rules\Doctrine\ORM\EntityRelationRule [1] [2] phpstan/phpstan-doctrine

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.