tags:

views:

11

answers:

1

Hi !

I have a Product table with a field to store the price. Each time, this price was changing, i need te keep the ancient value. So I have created a table called History to store the product id, the price and the date of changing. I've also created this method in my product class to generate a new entry :

public function updateHistory($modified) {      
    if(array_key_exists('price', $modified)) {
        $history = new History()
        $history = new History();$
        $history->setProductId($this->getId());
        $history->setPrice($modified['price']);
        $history->save()
    }
}

I call this method from the presave() method

public function preSave($event) {
    $modified = $this->getModified();
    $this->updateHistory($modified);
}

It works fine for edited product but not at all for a new product because it has no id at this time (product id is null in history table) and if i use updateHistory method in the postSave method, $modified is null;

So how can i create my first entry in the history table for a new product ?

Thanks all for you response and the time passed to help me !

(Sorry for my english)

A: 

Hello, if you want to easily keep track of the history of a product, you can use the "Versionable" behavior in your schema.yml file. This, way you will have an history with very few coding involved.

greg0ire