nkhanna24 Posted February 20, 2013 Share Posted February 20, 2013 hi all i Have a model question.php it has a method public function addActivity() { $subscribers = $this->getSubscribers(); foreach($subscribers as $id) { $n = new Activity(); $n->parentType = Activity::model()->parents['Question']; $n->parentId = $this->id; $n->userId = $id; $n->sourceId = Yii::app()->user->id; $n->activityType = Activity::model()->activities['post']; $n->save(); $user = User::model()->findByPk($id); if($id!=Yii::app()->user->id) $user->notificationsCount=$user->notificationsCount+1; $user->save(); } } this is aftersave() protected function afterSave() { parent::afterSave(); Tag::model()->updateFrequency($this->_oldTags, $this->tags, 'question'); if($this->isNewRecord) { $this->addActivity(); } } This works fine. But what i want to do now is to call addActivity() from another function from same model public function addActivityextend() { if($this->isNewRecord) { $this->addActivity(); } } Instead of aftersave(), i call this funtion from my controller like this $model=new question $model->addActivityextend(); But it doesnot work.Activity table is not populated in this case. but if i call directly $model->addActivity(); It works fine then. What am i missing?? Thankyou in advance Link to comment Share on other sites More sharing options...
Larry Posted February 20, 2013 Share Posted February 20, 2013 Logically, then, it would see that the $this->isNewRecord conditional is returning false in addActivityextend(), although that shouldn't be the case considering the code in your controller. But that's what I would first confirm. Link to comment Share on other sites More sharing options...
nkhanna24 Posted February 21, 2013 Author Share Posted February 21, 2013 Logically, then, it would see that the $this->isNewRecord conditional is returning false in addActivityextend(), although that shouldn't be the case considering the code in your controller. But that's what I would first confirm. no,this is not the case $this->isNewRecord is returning true and it calls the function.... Link to comment Share on other sites More sharing options...
Edward Posted February 21, 2013 Share Posted February 21, 2013 Aren't you missing the return statement in your afterSave method. Please ignore I made a mistake. Link to comment Share on other sites More sharing options...
Larry Posted February 22, 2013 Share Posted February 22, 2013 no,this is not the case $this->isNewRecord is returning true and it calls the function.... Could you be more specific? How do you know $this->isNewRecord is returning true. And when you say "it calls the function", what is "it" and what function? Have you tried doing a stack trace to see what functions are and are not being called, and in what order? Link to comment Share on other sites More sharing options...
nkhanna24 Posted February 23, 2013 Author Share Posted February 23, 2013 Could you be more specific? How do you know $this->isNewRecord is returning true. And when you say "it calls the function", what is "it" and what function? Have you tried doing a stack trace to see what functions are and are not being called, and in what order? i have checked and i am sure(100%) when i call addActivityextend() from controller $this->isNewRecord returns true and $this->addActivity() line works In adActivity() $subscribers = $this->getSubscribers(); this line works(it returns an array) After that i dont know why table does not update?? Link to comment Share on other sites More sharing options...
Recommended Posts