Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-2007, 08:21 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default Zend_Regitry and Arrays?

Interesting problem I can't seem to figure out.

I'm using Zend_Registry to store several Zend_Db_Table objects and a Zend_Config_Ini object. I set the Config_Ini object, a few dbAdapter objects and some of the table object in the bootstrap. To get any of these back I just have to call Zend_Registry::get('foo'); no problemo.

However, when I try to set a standard array things seem to break. The following code is not the exact code, just a mock up.

function setAction() {
$testArray = array(
'1' => 0,
'2' => 2,
'3' => 4,
etc....
);
Zend_Registry::set('testArray', $testArray);
}

function getAction() {
$testArray = Zend_Registry::get('testArray');
}

this keeps telling me that there is nothing in the registry with the key 'testArray'?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-06-2007, 10:23 PM
matias.quaglia's Avatar
Junior Member
 
Join Date: Aug 2007
Posts: 18
Default

Hi Elemental, i didn't tried ro reproduce exactly your error, but i did this:

in the bootstrap directly (no in a fuction, but directly in the bootstrap) I setted an array:

PHP Code:
$testArray = array(
    
'1' => 0,
    
'2' => 2,
    
'3' => 4,
);
$registry->set('testArray'$testArray); 
And then in an Action over controller I called that array on registry:

PHP Code:
$this->view->array Zend_Registry::get('testArray'); 
And in the view of that action:

PHP Code:
print_r($this->array); 
And that worked lovely, here is the output:

Code:
Array ( [1] => 0 [2] => 2 [3] => 4 )
Where do you use the setAction? where the getAction?

Cheers!
__________________
Matías Quaglia
==========
http://www.matiasquaglia.com.ar
Credo est Creo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 12:54 AM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

I use them in the same controller. So basically its a identity verification thingy.

In one action I get the username and password, and create a hash that is emailed to the user. I need to store the username/password/userid (a key in the table)/hash.

I think I figured out I was doing this all wrong for my purposes. I've since put it in the session namespace and all is working out fine now. However, I'd still like to find out why it wasn't working as I may need to register arrays in the future.

so,

class myController extends Zend_Controller_Action () {
public function startAction () {
$params = $this->_request->getParams('post');
$regInfo = array (
'id' = params['id'],
'uname' = params['username'],
'pass' = params['password'],
'vcode' = null,
);
$regInfo['vcode'] = md5($reginfo['id'] . $reginfo['password']);
//now I need to store this in the registry so I can refer to it later
Zend_Registry::set('regInfo', $regInfo);

//this works
$test = Zend_Registry::get('regInfo');
}

public function stopAction () {
//this doesn't
$test = Zend_Registry::get('regInfo');
}
}


stopAction throws an exception saying that there is no registered key 'regInfo'

Now, that's not how I am doing my authentication stuff anymore so its not a pressing issue. However, I would like to be set arrays to the registry in one action and recall them in another action within the same controller. Maybe I am not understanding how the registry handles non-objects?

Thanks for the reply and sorry for my horrid spelling... made me cringe to read it again, lol.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-07-2007, 03:06 AM
matias.quaglia's Avatar
Junior Member
 
Join Date: Aug 2007
Posts: 18
Default

Hi Elemental!

Well, I tried to do something like you.

I setted the array in an action, and requested it in another action of the same controller.

To the meanings of this example i created an one-index-dumby-array, storing a md5 of a string.
PHP Code:
    public function init()
    {
        
        
$myArray = array(
            
'hola' => md5('hola')
        );
        
$registry Zend_Registry::getInstance();
        
$registry->set('myArray'$myArray);
    } 
and here I use the array stored in the Registry to pass it to the view:

PHP Code:
    public function indexAction()
    {
        
$this->view->array Zend_Registry::get('myArray');
    } 
And the trivial view:

PHP Code:
print_r($this->array); 
Adn when i reach the example.com/controller/index i get:

Code:
Array ( [hola] => 4d186321c1a7f0f354b297e8914ab240 )
I also tried in the controller that sets the array in registry:

PHP Code:
public function init()
    {
    
        
$myArray = array(
            
'hola' => md5('hola')
        );
        
Zend_Registry::set('myArray'$myArray);
    } 
And also works fine.

May be you need get a closer look to your code, and doublecheck it, just in case...

Cheers, and you forgive me for my bad english
__________________
Matías Quaglia
==========
http://www.matiasquaglia.com.ar
Credo est Creo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-15-2007, 02:30 AM
Junior Member
 
Join Date: Aug 2007
Posts: 10
Default

Hi matias.quaglia,
I am a newbie in Zend Framework and I am facing the same problem with Elemental.
Does the init() place in same controller class with the indexAction() in your example. ?

If so, the registry must work, because init() would be called before the action().
thus, we place the registry::set() in the init() then each action() can access that registry value.

Q1) Can I place the registry::set() outside the init() (e.g. set registry in indexAction() and get by testAction() ) ?
Q2) How can I access that registred value in another controller class ?
Is it possible ?

thank you ~

Last edited by GeniusTse : 08-15-2007 at 02:34 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-15-2007, 04:22 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

You can do Zend_Registry::set() anywhere and Zend_Registry::get() anywhere. I'm not sure what was causing the issue I had before, but I am setting and getting arrays in other aspects of my app. Best example would be the config.xml. In my bootstrap I read in the config.xml via Zend_Config_Xml and put it in the registry. Then I retrieve it in a controller to setup a database connection or retrieve environment config settings.

The registry acts like a global varbiable store house so where you set and get don't matter, so long as your not trying to get something that hasn't been set...

If you could post some code snippets I bet we could help you out a bit better.

thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-16-2007, 01:39 AM
Junior Member
 
Join Date: Aug 2007
Posts: 10
Default Elemental Thank you for your reply

Here is my code, thank you ~

These code in a single controller class called TestController
PHP Code:
// Write a variable into registry
public function registryInAction()
    {
        
$value '123';
        
$registry = new Zend_Registry(array('index' => $value));
        
Zend_Registry::setInstance($registry);
        
        
        echo <<<EOH
                yoyo variable index has been registered!!<br />
EOH;

        
// get the index from registry
        
$registry Zend_Registry::getInstance();
        
var_dump$registry['index'] );
        
        
$this->_helper->viewRenderer->setNoRender();
    } 
Output: yoyo variable index has been registered!!
string(3) "123"

PHP Code:
// Try to read the value from registry
public function registryOutAction()
    {
        
// get the index from registry
        
$registry Zend_Registry::getInstance();

        
var_dump$registry['index'] );
        
        
$this->_helper->viewRenderer->setNoRender();
    } 
Output: NULL

do my codes have any mistake
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-16-2007, 04:46 AM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

Try this
Code:
// Write a variable into registry
public function registryInAction() {

  //build your array
  $value = array('index' => '123');

  //unless your using more than 1 registry just use the default instance
  $registry = Zend_Registry::getInstance();

  //set your array in the Registry
  Zend_Registry::set('test', $value);

  //a better test
  if($registry->isRegistered('test')) {
    echo ' yoyo variable index has been registered!!<br />';
  } else {
    echo 'dizam sumphin wentz wrongz!';
  }
  
  //clear out $value
  $value = null;

  // get the index from registry
  $value = Zend_Registry::get('test');
  var_dump($value);

  $this->_helper->viewRenderer->setNoRender();
}
Then this
Code:
// Try to read the value from registry
public function registryOutAction() {

  // get the index from registry
  $value = Zend_Registry::get('test');
  var_dump( $value );

  $this->_helper->viewRenderer->setNoRender();
}
I think you might have been confused about the difference between get()/set() and getInstance()/setInstance(). getInstance is basically a constructor for the singleton registry object. Meaning there is only one instance of the registry at any given time. You can create multiple instances but this is rarely needed. I have seen cases where the registry is setup, then destroyed and resetup, I"m not sure what's going on there...

Anyway, use set() and get() to store and retrieve variables, objects, arrays etc in the registry.

I didn't actually run the above code but it follows how I deal with the registry through out my app. Granted I did have some trouble with an array (see op) but I was confusing the registry for session data. See if this works and let me know the outcome.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-16-2007, 06:10 AM
Junior Member
 
Join Date: Aug 2007
Posts: 10
Default Thank you for your help ~

I copied your code and tried again ~
the output of the first action: registry-in
Code:
yoyo variable index has been registered!!
array(1) { ["index"]=> string(3) "123" }

but the second action: registry-out
Code:
Fatal error: Uncaught exception 'Zend_Exception' with message 
'No entry is registered for key 'test'' in 
/opt/lampp/lib/Zend/Registry.php:145

It seems that, the $value can be registry within the action registry-in and get correct inside, but cannot get it back from the action 'registry-out. Do I need to make some configurations let the registry work?

Out of topic:
On the other hand, I think that, my code is works when I was new an instance of Zend_Registry and apply Zend_Registry:setInstance. It is because that, there is the only object of Zend_Registry inside the Zend_Registry when singleton was applied. Of cause the previous registered registry will be destroy after setInstance() was called, but the it should be work if I never re-setInstance() , right?? If my concept is not correct please let me know because I am just a newbie in PHP and Zend framework ~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-16-2007, 08:34 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

ok that's the problem I was having originally...

I'm not sure where to go from there
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:55 AM.