alias
alias

Reputation: 155

working with DateTime object received from doctrine

I'm receive DateTime object from $lastPricing->getExpiredAt().

After that i'm try to save new entity object:

  $pricing = new AccountPricing();
  if ($currentPricing) {
      $start_date = $currentPricing->getExpiredAt();
  } else {
      $start_date = new \DateTime('now');
  }
  $pricing->setStartedAt($start_date);
  $this->entityManager->persist($pricing);
  $this->entityManager->flush();
  // This temporaly fix
  $start_date->modify('+ ' . $period . ' month');
  $pricing->setExpiredAt($start_date);
  $this->em->persist($pricing);
  $this->em->flush();

If I try this:

  $pricing = new AccountPricing();
  if ($currentPricing) {
      $start_date = $currentPricing->getExpiredAt();
  } else {
      $start_date = new \DateTime('now');
  }
  $pricing->setStartedAt($start_date);
  $start_date->modify('+ ' . $period . ' month');
  $pricing->setExpiredAt($start_date);
  $this->em->persist($pricing);
  $this->em->flush();

In started_at and expired_at writed similar dates (those that after the modify).

I think this because DateTime is objects and in php all object passing by reference.

Have ideas how I can do this without double flush?

Upvotes: 0

Views: 47

Answers (1)

alias
alias

Reputation: 155

I forget about clone:

  $pricing = new AccountPricing();
  if ($currentPricing) {
      $start_date = $currentPricing->getExpiredAt();
  } else {
      $start_date = new \DateTime('now');
  }
  $pricing->setStartedAt($start_date);
  $end_date = clone $start_date;
  $end_date->modify('+ ' . $period . ' month');
  $pricing->setExpiredAt($end_date);
  $this->em->persist($pricing);
  $this->em->flush();

Upvotes: 1

Related Questions