Yii Framework Forum: Problem with application wide component - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Problem with application wide component Component seems to be re-instantiated each time it is accessed Rate Topic: -----

#1 User is offline   DigiFox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 30-January 11

Posted 21 August 2012 - 02:46 PM

Hi all,

Based on the wiki (this article)) I wrote a simple application wide component:

<?php

class XChangeSingleton extends CApplicationComponent {

    private $_case_id = null;

    public function setCaseID($case_id) {
        $this->_case_id = $case_id;
    }

    public function getCaseID() {
        if (!$this->_case_id) {
            $this->_case_id = 0;
        }
        return $this->_case_id;
    }

}

?>


In main.php I put this code to create an object of that class:

	'components'=>array(
                'xchange'=>array('class'=>'XChangeSingleton'),
                ....


Then I use the following statements to access the component:

Yii::app()->xchange->setCaseId($cur_case_id);
...
 $model->case_id = Yii::app()->xchange->CaseId; 


But the call xchange->CaseId in all cases returns 0. If I follow the flow with the debugger it seems that Yii does not recognize that the xchange component exists and thus creates a new one. Your thoughts on this are highly appreciated. Is it a horrible typo or am I missing something? I am using yii-1.1.11.58da45.

DigiFox
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 21 August 2012 - 03:05 PM

I checked your code in my localhost.No problem for me.
0

#3 User is offline   DigiFox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 30-January 11

Posted 21 August 2012 - 03:25 PM

View Postseenivasan, on 21 August 2012 - 03:05 PM, said:

I checked your code in my localhost.No problem for me.


Thanks for the quick response, pity it worked for you :) !

Update: when I set a value for CaseID and retrieve it in the next statement it seems to work. But when I set a value in one action of a controller (Index) and retrieve it in another (Create) it does not work. In that last case a new xchange-object is created.

DigiFox
0

#4 User is offline   phtamas 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 529
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 22 August 2012 - 12:08 AM

View PostDigiFox, on 21 August 2012 - 03:25 PM, said:

But when I set a value in one action of a controller (Index) and retrieve it in another (Create) it does not work. In that last case a new xchange-object is created.


That's the way PHP works. The lifespan of a PHP object is (at most) one HTTP request. Store your data in the session if you want it to survive the request.
0

#5 User is offline   DigiFox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 30-January 11

Posted 22 August 2012 - 12:44 AM

View Postphtamas, on 22 August 2012 - 12:08 AM, said:

That's the way PHP works. The lifespan of a PHP object is (at most) one HTTP request. Store your data in the session if you want it to survive the request.


Ah, that was stupid. Completely forgot about this, no state. Thanks for taking the time to clear this up, phtamas!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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