0

I have the following entity:

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;

class Quote
{
    use SourceTrait;
    use TimestampableEntity;

    private $quoteId;

    private $startDate;

    private $endDate;


    public function getStartDate(): ?\DateTime
    {
        return $this->startDate;
    }


    public function setStartDate(\DateTime $startDate)
    {
        $this->startDate = $startDate;
    }

    public function getEndDate(): ?\DateTime
    {
        return $this->endDate;
    }


    public function setEndDate(\DateTime $endDate)
    {
        $this->endDate = $endDate;
    }

}

and I have this method in the QuoteRepository class:

public function createQuoteHeader(Agreement $agreement): int
{
    $em      = $this->getEntityManager();
    $QuoteID = $this->getNewQuoteId();
    $year    = $agreement->getEndDate()->diff($agreement->getStartDate(), true)->y > 0
        ? $agreement->getEndDate()->diff($agreement->getStartDate(), true)->y
        : 0;

    dump($year);
    dump(gettype($year));
    dump($agreement->getEndDate());

    $entity = new Quote();
    ...
    $entity->setStartDate($agreement->getEndDate()->modify('+1 day'));
    dump($agreement->getStartDate());
    $entity->setEndDate($agreement->getEndDate()->modify("+1 day +{$year} year"));
    dump($agreement->getEndDate());
    ...

    $em->persist($entity);
    $em->flush();

    return $entity->getQuoteId();
}

The output from the dump() above are as follow (same order as in code):

0
"integer"
DateTime {#3005
  +"date": "2017-03-21 00:00:00.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#3004
  +"date": "2016-03-22 00:00:00.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#3005
  +"date": "2017-03-23 00:00:00.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}

Somehow the wrong values are being inserted in DB:

enter image description here

Maybe I am missing something but should not be StartDate equal to 2016-03-22 00:00:00??

2 Answers 2

2

Let's walk through your code line by line.

$entity->setStartDate($agreement->getEndDate()->modify('+1 day'));

modify() mutates the DateTime object it is called upon. This line added +1 day to $agreement->getEndDate() AND the same DateTime is passed to $entity->setStartDate(). So BOTH $agreement->getEndDate() and $entity->getStartDate() will return 2017-03-22!

dump($agreement->getStartDate());

You dumped the wrong date, hence 2016-03-22, masking the error. Should be dump($entity->getStartDate()); and you would have noticed.

$entity->setEndDate($agreement->getEndDate()->modify("+1 day +{$year} year"));

Since $agreement->getEndDate() is now equal to 2017-03-22, +1 day +0 year will result in $entity->getEndDate() being equal to 2017-03-23.

I recommend using DateTimeImmutable to avoid all these unnecessary issues with object references.

Sign up to request clarification or add additional context in comments.

Comments

1

It seems to be a problem with the object you are trying to persist. You are getting the right result when you make dump(), but the datetime object is being modified later, so when you persist the object, all of the values get your last datetime result. I recommend you to make "clone" in your datetime objects, and set the values separetely, something like this:

$oDatetime1  = clone($agreement->getEndDate());
$oDatetime2  = clone($agreement->getEndDate());

$entity->setStartDate($oDatetime1->modify('+1 day'));
$entity->setEndDate($oDatetime2->modify("+1 day +{$year} year"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.