Symfony2 - Problems with Doctrine2's PreUpdate

0

My entity Budget has some methods that run on PrePersist and PreUpdate . They are:

/**
 * @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 generateNextPaymentDate() method is usually executed and uses the value of the starts_at field, a DateTime field, of the Budget entity. By inserting a new one or updating an existing one, the method works normally, having its value returned / inserted into the database.

The calculateInstallmentRatePrice() and calculateTotalBudgetPrice() methods use the value of the meters field, an integer type field, of the Product entity, which is a collection form embedded in the Budget form %. The methods work, but it is necessary that I have changed some field of the main form, any other value, so that these methods have their values persisted in the database. Otherwise, they are usually executed but are not updated.

I do not know how to fix this. Should I have forgotten something?

    
asked by anonymous 02.11.2015 / 02:07

0 answers