Symfony2 - Problems with Doctrine2 Preupdate

Asked

Viewed 30 times

0

My entity Budget has some methods that are executed in PrePersist and PreUpdate. It’s them:

/**
 * @return \DateTime
 */
public function generateNextPaymentDate()
{
    if ($this->getStartsAt() !== null) {
        $date = new \DateTime($this->getStartsAt()->format('Y-m-d'));
        return $date->add(new \DateInterval('P' . $this->getCheckFor() . 'D'));
    }
}

/**
 * @return decimal
 */
public function calculateTotalBudgetPrice()
{
    $totalBudgetPrice = 0;

    foreach ($this->getItems() as $item) {
        $totalBudgetPrice += $item->getPrice();
    }

    return $totalBudgetPrice;
}

/**
 * @return decimal
 */
public function calculateInstallmentRatePrice()
{
    return $this->calculateTotalBudgetPrice() / $this->getInstallmentRate();
}

/**
 * @ORM\PrePersist
 * @ORM\PreUpdate
 */
public function onPreEvents()
{
    $this->setNextPaymentDate($this->generateNextPaymentDate());
    $this->setInstallmentRatePrice($this->calculateInstallmentRatePrice());
    $this->setTotalBudgetPrice($this->calculateTotalBudgetPrice());
}

The method generateNextPaymentDate() is normally executed and uses the field value starts_at, a field of sorts DateTime, of the entity Budget. Inserting a new one or updating an existing one, the method works normally, having its value returned inserted/updated in the bank.

Already the methods calculateInstallmentRatePrice() and calculateTotalBudgetPrice() use the field value meters, a whole type field of the entity Product, which is a Collection form embedded in the form Budget. The methods work, but it is necessary that I have changed some field of the main form, any other value, for these methods to have their values persisted in the bank. Otherwise, they are normally executed but are not updated.

I don’t know how to fix it. I must have forgotten something?

  • You mean that the first time the entity is saved the values are not calculated; but the next times, yes?

  • @Rodrigorigotti Not exactly. Independent of being the first edition or the tenth of the same budget, if I want to modify the amount of meters of a material that that budget has, currently I also need to change some other field of the budget form, be it the initial date, or number of installments, anything to modify in the database the values returned from those two methods.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.