I am looking for a way to improve a model I have created.
What I am doing is using the model to create new pricing information on the fly, and set this information along with expiry times. The approach I am using is to offload some of the timing operations to the Database server using a Zend_Db_Expr to achieve this.
The problem I have is that after doing this I need to further make use of the model itself for further purposes. This means I need to get the actual result of the Expression inserting in to the database.
I have removed some of the code from this example as its business logic and should not impact on the question or answer anyway.
class My_Module_Model_Price extends Mage_Core_Model_Abstract {
public function getPrice()
{
$this->addData(
array(
'current_price' => $this->getSpecialPrice(),
'product_id' => $this->_product->getId(),
'price_from' => new Zend_Db_Expr('NOW()'),
'price_to' => new Zend_Db_Expr('DATE_ADD(NOW(), INTERVAL ' . $endTime . " MINUTE)"),
)
);
$this->save();
}
public function _afterSave()
{
if ($this->getData('price_from') instanceof Zend_Db_Expr) {
$this->load($this->getData('id'));
}
}
This logic gives me what I require, i.e. after calling it I am able to use $model->getData() and this returns the fields generate db the DB Expressions but I feel like I am doing an extra DB Query round for no useful reason just to get back some information. So my goal would be to convert the _afterSave such that I dont need it at all, or that I dont need to trigger a load within the model.