Related models are not reloaded when transaction is on


(Mar Czapla) #1
  • operating system: Linux Ubuntu

  • Web server: apache2

  • browser type: any

  • Yii version: 1.1.13

I have some $model, which has a relation




        return array(

            'costCentre' => array(self::BELONGS_TO, 'CostCentre', 'cost_centre_id'),

        );



When I load model outside the transaction (mySQL innoDB) and change the attribute $model->cost_centre_id then the related model acessed by $model->costCentre will be reloaded and different costCentre will be there.




$model                 = $this->loadModel($id); // $model->cost_centre_id = 1

$model->cost_centre_id = 2;

echo $model->costCentre->cost_centre_id; // 2



however when transaction is on then changing attribute $model->cost_centre_id doesn’t force relation to be reloaded and previous model is returned when using this relation




$model                 = $this->loadModel($id); // $model->cost_centre_id = 1

$transaction           = Transaction::beginTransaction();

$model->cost_centre_id = 2;

echo $model->costCentre->cost_centre_id; // 1 <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />






    /**

     * Does after change actions

     */

    public function afterSave()

    {

        if (!empty($this->_previousCostCentre) && $this->_previousCostCentre->cost_centre_id != $this->cost_centre_id) {

            $this->_previousCostCentre->recalculateBalance();

            $this->_previousCostCentre->save();

        }


        // This code below works different when transaction is OFF and ON

        if (!empty($this->costCentre)) {

            $this->costCentre->recalculateBalance();

            $this->costCentre->save();

        }


        parent::afterSave();

    }



Regards

Mario