Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
nkhanna24

Calling Model Function

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

Share this post


Link to post
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.

Share this post


Link to post
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....

Share this post


Link to post
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?

Share this post


Link to post
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??

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...