Code Monkey home page Code Monkey logo

Comments (11)

RichAtPowderBlue avatar RichAtPowderBlue commented on June 14, 2024

I'm having the exact same problem. Did you find a workaround (other than just ignoring those errors)?

from phpstan-doctrine.

noemi-salaun avatar noemi-salaun commented on June 14, 2024

No I didn't find a good solution. I gived up and add it to the ignored errors :/

from phpstan-doctrine.

lookyman avatar lookyman commented on June 14, 2024

I will try to look into it.

from phpstan-doctrine.

RichAtPowderBlue avatar RichAtPowderBlue commented on June 14, 2024

Thanks for replying @noemi-salaun I've ended up doing the same. Also thanks @lookyman - really appreciate it 😄

from phpstan-doctrine.

lukasluecke avatar lukasluecke commented on June 14, 2024

I have a similar issue, except my use-case is the opposite - I have a non-nullable database field, but need the property itself to be nullable for creation (and then use a NotNull assert to prevent possible issues).

from phpstan-doctrine.

ThomasLandauer avatar ThomasLandauer commented on June 14, 2024

@noemi-salaun I have the same, but with an additional @ORM\JoinColumn(nullable=false) on UserStats::$user. This tells Doctrine that there can be no UserStats with an empty user column. This aspect might already be included in @ORM\Id though ;-)

I think the real problem is: From looking at the mapping, you cannot answer the following question:

When a User is created, will there be a UserStats created in any case?

...cause the mapping does allow a User without a UserStats. In my app, I can answer with YES, cause in Users constructor I have:

$this->setStats(new UserStats($this));

... and cascade={"persist"}) on User::$stats.

@lookyman Checking for those two aspects might be an idea to resolve this.

Doctrine docs: https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity

from phpstan-doctrine.

mitelg avatar mitelg commented on June 14, 2024

I have a similar problem

class Order
{
    /**
     * @var int
     *
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(name="id", type="integer", nullable=false)
     */
    private $id;

    /**
     * @var Billing
     *
     * @ORM\OneToOne(targetEntity="Billing", mappedBy="order", orphanRemoval=true, cascade={"persist"})
     */
    private $billing;

   // ...
}

class Billing
{
    /**
     * @var int
     *
     * @ORM\Id()
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var Order
     *
     * @ORM\OneToOne(targetEntity="Order", inversedBy="billing")
     * @ORM\JoinColumn(name="order_id", referencedColumnName="id", nullable=false)
     */
    private $order;

    /**
     * @var int
     *
     * @ORM\Column(name="order_id", type="integer", nullable=false)
     */
    private $orderId;

    // ....
}

PHPStan error message:

Property Order::$billing type mapping mismatch: database can contain Billing|null but property expects Billing

from phpstan-doctrine.

ondrejmirtes avatar ondrejmirtes commented on June 14, 2024

@mitelg If there's no Billing for order ID 123 in the database, the $billing property for Order 123 will be null.

from phpstan-doctrine.

mitelg avatar mitelg commented on June 14, 2024

@ondrejmirtes thanks for your reply

but an order without billing is not valid and should/could not happen 🤔 at least, I hope so 😅🙈

is there a way to ensure that?

I would have to look it up tomorrow, but on DB level, this should be prevented.

from phpstan-doctrine.

ondrejmirtes avatar ondrejmirtes commented on June 14, 2024

I think it can't be enforced on the DB level.

from phpstan-doctrine.

mitelg avatar mitelg commented on June 14, 2024

hey @ondrejmirtes

I had a deeper look in the code, and the order and the billing are created directly via SQL 🙈
So due to erroneous behaviour at some point, it could indeed be possible that an order has no billing in the respective doctrine model 😬

legacy software is fun... 🎉

thank you very much again!

from phpstan-doctrine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.