Search code examples
phpcakephppluginscakephp-3.0cakedc

How to extend Cakedc Users plugin to use own table in Cakephp 3


I want to extend users plugin so that i can remove username field and customize my table according to needs. I have seen the github documentation for extending plugin but that doesn't help me much. I tried to extend UserTable.php but it gives me error Unknown method "register". Please suggest best way or simple code.


Solution

  • Check this cloud9 environment created specifically to show how to extend the model and table as an example: https://ide.c9.io/steinkel/users-example-custom-table

    • The example uses a custom table my_users
    • The example uses a custom Table class MyUsersTable extending the CakeDC/Users.Users table
    • Keep in mind the field 'username' is used in SocialLogin and other features by default, if you are going to remove it ensure you configure the app to rely on another field.

    Basically changes done to use a custom table are:

    • Create a new database table, matching the original fields and add/remove fields based on your needs
    • Create a new Table class in your app, extending the CakeDC/Users.Users table
    • Override required methods, like validation or finders in your table class
    • Set your table in configuration key 'Users.table' like we did in 'config/users.php'

      $config = [
          'Users' => [
              //Table used to manage users
              'table' => 'MyUsers',
          ] 
      ]; 
      return $config;
      

    And you are done :)