Yii Framework Forum: can't retrieve related ar object record from newly inserted ar object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

can't retrieve related ar object record from newly inserted ar object Rate Topic: -----

#1 User is offline   Yuest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 07-December 08
  • Location:China

Posted 15 December 2008 - 05:40 AM

is this a bug?
i use it in afterSave() to implement a common fuction:

public function afterSave()
{
    $this->post->replyQty += 1;
    $this->post->save();
}

it raise a exception says that $this->post is null
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,875
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 December 2008 - 11:55 AM

What is $this->post? Is it loaded before you call its save()?
0

#3 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 15 December 2008 - 01:46 PM

Beware pf going into endless loops, Yuest.  You are putting a save in afterSave(), which in turn calls another afterSave(). !
0

#4 User is offline   Yuest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 07-December 08
  • Location:China

Posted 16 December 2008 - 01:44 PM

<?php

class Reply extends CActiveRecord
{
    //...

    public function relations()
    {
        return array(
            'author'=>array(self::BELONGS_TO, 'User', 'userId'),
            'post'=>array(self::BELONGS_TO, 'Post', 'postId'),
        );
    }
    //...

    /*
    protected function afterSave()
    {
        $this->post->replyQuantity += 1;
        $this->post->lastReplyTime = date("Y-m-d H:i:s");
        $this->post->save();
    }
    * this doesn't works, i must use the code below: */
    protected function afterSave()
    {
        $post = Topic::model()->findbyPk($this->postId);
        $post->replyQuantity += 1;
        $post->lastReplyTime = date("Y-m-d H:i:s");
        $post->save();
    }


    protected function afterDelete()
    {
        $this->post->replyQuantity -= 1;
        $this->post->save();
    }//this works when deleting
}

0

#5 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,875
  • Joined: 04-October 08
  • Location:DC, USA

Posted 16 December 2008 - 03:31 PM

Thanks. This is fixed in SVN.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users