TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
KO3: Session/orm bug. Undefined index: id
  • I have a problem with maybe sessions but the last error is from the ORM library. I read in this forum that this bug is repeared but i still have problem with Undefined index: id.

    The screen from the firefox with this stack trace: image
  • Enyone know how to repear that??
  • I also get this error when using latest ORM with latest A1 module
  • Despite claims in other threads it appears to me that this issue is not fixed in the current version of ORM on github. Working with k3 and ORM updated on Jan 15th and A1 updated on Nov 25th I get this error when returning to a site with an existing cookie/session for a logged in user after closing the browser and opening it again. Happens in all browsers. [edit] after clearing browser cookies the error does not recur when the page is reloaded.

    ErrorException [ Notice ]: Undefined index: id
    MODPATH/orm/classes/kohana/orm.php [ 1308 ]
    1303 *
    1304 * @return bool
    1305 */
    1306 protected function empty_pk()
    1307 {
    1308 return (empty($this->_object[$this->_primary_key]) AND $this->_object[$this->_primary_key] !== '0');
    1309 }
    1310
    1311 /**
    1312 * Returns last executed query
    1313 *
    MODPATH/orm/classes/kohana/orm.php [ 1308 ] » Kohana_Core::error_handler()
    MODPATH/orm/classes/kohana/orm.php [ 729 ] » Kohana_ORM->empty_pk()
    MODPATH/orm/classes/kohana/orm.php [ 273 ] » Kohana_ORM->_load()
    {PHP internal call} » Kohana_ORM->__call(arguments)
    MODPATH/a1/classes/a1.php [ 118 ] » Model_User->loaded()
    APPPATH/classes/controller/base.php [ 38 ] » A1->get_user()
    APPPATH/classes/controller/website.php [ 27 ] » Controller_Base->before()
    {PHP internal call} » Controller_Website->before()
    SYSPATH/classes/kohana/request.php [ 857 ] » ReflectionMethod->invoke(arguments)
    APPPATH/bootstrap.php [ 168 ] » Kohana_Request->execute()
    DOCROOT/index.php [ 103 ] » require(arguments)
  • Incase it helps track down the issue, I also got the exact same error when stupidly omitting find_all like so:
    ORM::factory('houses')->as_array();
  • I've seen this happen a few times when the cookie (where I'm storing session data) gets corrupted, and the ORM isn't able to wakeup successfully. I'm not really sure if this is an ORM problem (A1 maybe)?, or what is causing it. I haven't been able to reproduce the problem, however if anyone is able to provide more information and how they can reproduce this issue it would be easier to figure out what we can do to fix it.
  • Although it's a mistake to leave out find_all from here, this line reproduces a very similar stack trace:
    echo kohana::debug(ORM::factory('ormmodelname')->as_array());

    ErrorException [ Notice ]: Undefined index: id
    MODPATH/orm/classes/kohana/orm.php [ 1308 ]
    1303 *
    1304 * @return bool
    1305 */
    1306 protected function empty_pk()
    1307 {
    1308 return (empty($this->_object[$this->_primary_key]) AND $this->_object[$this->_primary_key] !== '0');
    1309 }
    1310
    1311 /**
    1312 * Returns last executed query
    1313 *
    MODPATH/orm/classes/kohana/orm.php [ 1308 ] » Kohana_Core::error_handler()
    MODPATH/orm/classes/kohana/orm.php [ 729 ] » Kohana_ORM->empty_pk()
    MODPATH/orm/classes/kohana/orm.php [ 311 ] » Kohana_ORM->_load()
    MODPATH/orm/classes/kohana/orm.php [ 570 ] » Kohana_ORM->__get(arguments)
    APPPATH/classes/controller/home.php [ 10 ] » Kohana_ORM->as_array()
    {PHP internal call} » Controller_Home->action_index(arguments)
    SYSPATH/classes/kohana/request.php [ 863 ] » ReflectionMethod->invokeArgs(arguments)
    APPPATH/bootstrap.php [ 168 ] » Kohana_Request->execute()
    DOCROOT/index.php [ 103 ] » require(arguments)
  • I have this problem after application crush (fatal error), and only cookie clearing helps.

    It isn't (only) ORM problem, but ORM::empty_pk() should check $this->_object[$this->_primary_key] for existence.
  • This was already reported here :
    http://dev.kohanaphp.com/issues/2231

    You can reopen that ticket... At that time I was unable to reproduce that issue, so the ticket get closed. Thanks for your input, biakaveron.
  • Doing ORM::factory('model')->as_array() isn't failing for me with the latest checkout - can you verify this issue still exists?
  • I'm still experiencing this issue far too regularly. I don't get it from doing ORM::factory('model')->as_array() though. I'd say the problem occurs with the session and as a result the ORM __wakeup() fails, since clearing the session resolves the issue for me.
  • This affects me as well. I did some debugging and it looks it has something to do with corrupted session data.

    When it works, session data looks like this:

    last_active|i:1264586990;pcsy_auth|O:10:"Model_User":6:{s:15:"*_object_name";s:4:"user";s:10:"*_object";a:6:{s:2:"id";i:1;s:5:"email";s:9:"[email protected]";s:8:"username";s:6:"zimage";s:8:"password";s:50:"ceaa...";s:6:"logins";i:67;s:10:"last_login";i:1264586964;}s:11:"*_changed";a:0:{}s:10:"*_loaded";b:1;s:9:"*_saved";b:1;s:11:"*_sorting";a:1:{s:2:"id";s:3:"ASC";}}

    And when it says "Undefned index: id" the seeions data looks like this:

    last_active|i:1264581783;pcsy_auth|O:10:"Model_User":6:{s:15:"*_object_name";s:4:"user";s:10:"*_object";a:0:{}s:11:"*_changed";a:0:{}s:10:"*_loaded";b:1;s:9:"*_saved";b:1;s:11:"*_sorting";a:1:{s:2:"id";s:3:"ASC";}}

    _object is an empty array, so it doesn't have the 'id' key, which is looked for... I still don't know why the session looks like this, but anyway orm should handle this situation more gracefully, doesn't it?
  • I have been experiencing this bug as well.

    It happens to me when some kinda error occurs ( you see the stack trace etc.. ) then when I refresh i get the Undefined index: id error.

    by the way i'm using php5.1.6 (don't ask...)

    EDIT: just tried to run my app on php5.3 and it seems I'm not getting the error any more.
  • What's the status on this bug? It is still present in KO 3.0.4 with the most recent versions of ORM and Auth taken from Github.

    The bug-report seems to have had no activity the past 6 months: http://dev.kohanaframework.org/issues/2231

    Is there anybody who at least has a work-around? The bug appears on /modules/orm/classes/kohana/orm.php, line 1316. I am thinking of changing the original code:


    /**
    * Returns the value of the primary key
    *
    * @return mixed primary key
    */
    public function pk()
    {
    return $this->_object[$this->_primary_key];
    }


    To something like:


    /**
    * Returns the value of the primary key
    *
    * @return mixed primary key
    */
    public function pk()
    {
    if(isset($this->_object[$this->_primary_key]))
    {
    return $this->_object[$this->_primary_key];
    }
    else
    {
    // Session is broken, have the user login again for a fresh session
    Request::instance()->redirect('/login');
    }
    }


    EDIT: Well, the above code doesn't even seem to work :( /EDIT

    This way at least the user has the minor inconvenience of loggin in again, instead of seeing a crashed site...

    But I hate to remember to insert this code again when I update Kohana or the ORM module. Any better solutions out there?
  • I experienced this problem as well. My workaround is to destroy the corrupted session:
    try
    {
    $this->session = Session::instance();
    } catch(ErrorException $e) {
    session_destroy();
    }

    After the session has been destroyed the later part of my Controller:before method thinks the user is not logged in and redirects him to the login screen:
    $currentPage = Request::instance()->uri . Url::query();
    $loginUrl = 'account/login?'.http_build_query(array('redirect'=>$currentPage), '', '&');
    Request::instance()->redirect($loginUrl);

    After logging in the user is redirected right to the page the error occurred on.
  • im experiencing this with kohana 3.0.7, is there a way to fix it or do i have to add try {} everywhere ?
  • Also experiencing this problem using 3.0.7

    I can't find a workaround for this.
  • Same here. Still experiencing this Session-related problem in 3.0.7. I noticed that the request directly before the one that leads to the error named in the topic, another one occurs ... saying that no connection to the database can be established.
  • Same as @peta.
    Anyone?
  • Clear your cookies to reset your session. This is a known bug in PHP involving __sleep not being called when you have a fatal error. We are working on a work around / fix. For now just clear your cookie/session when it happens.
  • I had the same problem working with the Auth module, figured out the session gets a corrupted ORM object (doesn't make the find() call) and this was caused to an error while trying to use the model (my own fault). To solve this, I just added

    protected $_reload_on_wakeup = false;

    to Model_Auth_User (or whichever model you are having the problem with) and destroyed the session (or cookie). Then debug your code. Once your code is in working order, you can either set the above line to true, or just delete it (true is default) and everything works again.
  • the solution is to make sure the database object in the model is cleared when the model runs __wakeup() this is also filed as a bug and fixed if you search a bit... too lazy to find link, proper solution by Isaiah is to implement serializable interface because PHP __sleep() is broken
  • Any help on what Zeelot3k meant (what exactly to do with the model)? I have added the fix that czukowski referenced but now I just get logged off every time my page loads. I really need to find out where the fatal error is.