Jump to content
Larry Ullman's Book Forums

Calling Model Function


nkhanna24
 Share

Recommended Posts

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

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

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

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

 Share

×
×
  • Create New...