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

Type matching mismatch in child class with SINGLE TABLE INHERITANCE #493

Open
DesLynx opened this issue Nov 17, 2023 · 3 comments
Open

Type matching mismatch in child class with SINGLE TABLE INHERITANCE #493

DesLynx opened this issue Nov 17, 2023 · 3 comments

Comments

@DesLynx
Copy link

DesLynx commented Nov 17, 2023

Hi!

In the case of SINGLE TABLE INHERITANCE the doctrine documentation states that:

For Single-Table-Inheritance to work in scenarios where you are using either a legacy database schema or a self-written database schema you have to make sure that all columns that are not in the root entity but in any of the different sub-entities has to allow null values. Columns that have NOT NULL constraints have to be on the root entity of the single-table inheritance hierarchy.

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?

@ondrejmirtes
Copy link
Member

Please show a piece of code that is affected by this behaviour, I'm having a hard time imagining it without an example.

@DesLynx
Copy link
Author

DesLynx commented Nov 17, 2023

<?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:

 ------ --------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/Entity/ChildClass.php                                                                                                                          
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------- 
  XX       Property App\Entity\ChildClass::$mandatoryFieldInChildClass type mapping mismatch: database can contain string|null but property expects string.  
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------- 

@stof
Copy link
Contributor

stof commented Aug 23, 2024

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants