-
Notifications
You must be signed in to change notification settings - Fork 97
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
Type matching mismatch in child class with SINGLE TABLE INHERITANCE #493
Comments
Please show a piece of code that is affected by this behaviour, I'm having a hard time imagining it without an example. |
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: BaseEntityRepository::class)]
#[ORM\InheritanceType(value: 'SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
#[ORM\DiscriminatorMap(value: [
'child_class' => ChildClass::class, // just one for the exemple
])]
abstract class BaseEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;
#[ORM\Column(type: 'string')]
protected string $commonField;
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): static
{
$this->id = $id;
return $this;
}
public function getCommonField(): string
{
return $this->commonField;
}
/**
* @param string $commonField
*
* @return static
*/
public function setCommonField(string $commonField): static
{
$this->commonField = $commonField;
return $this;
}
}
#[ORM\Entity(repositoryClass: ChildClassRepository::class)]
class ChildClass extends BaseEntity
{
// this field is mandatory in the logic of the app but exists only in this child class so must be nullable in the database
#[ORM\Column(type: 'string', nullable: true)]
private string $mandatoryFieldInChildClass;
public function getMandatoryFieldInChildClass(): string
{
return $this->mandatoryFieldInChildClass;
}
public function setMandatoryFieldInChildClass(string $mandatoryFieldInChildClass): self
{
$this->mandatoryFieldInChildClass = $mandatoryFieldInChildClass;
return $this;
}
} Generated error:
|
well, phpstan is right here: the database can contain null values. You might decide to ignore this error (if you know that nothing else than the ORM in the project will ever write in that table), but it does not remove the fact that phpstan is right about reporting this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
In the case of SINGLE TABLE INHERITANCE the doctrine documentation states that:
So I think that the analysis shouldn't report an "type matching mismatch" for fields in child class that are not nullable even if the database field can be NULL.
What is your opinion on that matter?
The text was updated successfully, but these errors were encountered: