phpkohanakohana-3.3

Kohana Model_Auth_User_Token not getting overridden


PDO doesn't support the list_columns function. To get around this when using PDO with Kohana's Auth module, you simply have to list the table names as array keys in an extension of the model class, thusly:

<?php defined('SYSPATH') OR die('No Direct Script Access');

class Model_User extends Model_Auth_User
{
    protected $_has_many = array(
            'roles' => array(
                    'through' => 'roles_users',
                    'foreign_key' => 'user_id',
                    'far_key' => 'role_id'
            )
    );
    protected $_table_columns = array(
        'id' => null,
        'username' => null,
        'password' => null,
        'email' => null,
        'logins' => null,
        'last_login' => null
    );
}

However, my user_tokens model class is returning true from empty($this->_table_columns), causing Kohana to attempt to call list_columns, which throws an exception.

On the same page load, both my users model class and my roles model class are behaving as intended; the process is hiccuping when it hit the user_tokens. I did some digging, using var_dump to check on things; my first hint was that adding another property to my model class didn't show up in the vardump. After some more digging, I finally dropped in a call to ReflectionClass::getFileName to make sure my implementation of the class was actually being used.

The results produced APPPATH\classes\Model\User.php for my users model class, APPPATH\classes\Model\Role.php for my roles model class... but I hit MODPATH\orm\classes\Model\User\Token.php for my user_tokens model class!

There is little different between my implementation of the three models beyond the actual columns being used. My users implementation is above, and here are my roles and user_tokens implementations:

<?php defined('SYSPATH') OR die('No Direct Script Access');

class Model_Role extends Model_Auth_Role
{
    protected $_table_columns = array(
        'id' => null,
        'name' => null,
        'description' => null
    );
}
<?php defined('SYSPATH') OR die('No Direct Script Access');

class Model_User_Token extends Model_Auth_User_Token
{
    protected $_table_columns = array(
            'id' => null,
            'user_id' => null,
            'user_agent' => null,
            'token' => null,
            'created' => null,
            'expires' => null
    );
}

And here's the version of Model_User_Token from MODPATH:

<?php defined('SYSPATH') OR die('No direct access allowed.');

class Model_User_Token extends Model_Auth_User_Token {

    // This class can be replaced or extended

} // End User Token Model

I don't understand why my application is pulling Model_User_Token from MODPATH instead of APPPATH, especially when Model_User and Model_Role are getting picked up correctly (MODPATH contains similarly empty implementations of both).

How can I get Kohana to pick up my application version of the class, instead of the module version of the class?


Solution

  • You have to follow the kohana autoloader's naming convention guidelines in order for it to find your file. This can vary depending on the version of Kohana:

    for Kohana <= 3.2

    The class class My_Class_Name {} should be in a file named classes/my/class/name.php within your APPPATH directory (all lowercase directories and file names with underscores replaced by /).

    For Kohana >= 3.3

    The class class My_Class_Name {} should be in a file named classes/My/Class/Name.php within your APPPATH directory (PSR-0 format, underscores replaced by / and directories and file names match class name case).