How do I use a custom table instead of the standard CakeDC/Users table?

235 views Asked by At

I'm using CakePHP with the CakeDC/Users plugin, and I want to store more than just the standard user details on registration. I thought I had the answer here: https://github.com/CakeDC/users/blob/master/Docs/Documentation/Extending-the-Plugin.md#extending-the-model-tableentity, but that flat out doesn't make sense to me anymore.

I set it the site as suggested in the documentation:

  • my new table name was site_users and I included all the standard 'users' table fields, along with the additional fields I needed.
  • I created the new Model/Table and Model/Entity classes
  • I created a new register.ctp template

...but on registration, it was still attempting to make changes to the standard users table, instead of my new site_users table.

Here is the new Entity File, which includes the list of fields in the site_users table:

namespace App\Model\Entity;
use CakeDC\Users\Model\Entity\User;

/**
 * SiteUser Entity
 *
 * @property int $id
 * @property string $first_name
 * @property string $last_name
 * @property string $email
 * @property string $company
 * @property string $abn
 * @property string $ngr
 * @property \Cake\I18n\Time $date_registered
 * @property string $street_address
 * @property string $suburb
 * @property string $region
 * @property string $state
 * @property string $postcode
 * @property string $mobile
 * @property string $landline
 * @property string $fax
 * @property string $username
 * @property string $password
 * @property string $receive_marketing
 * @property string $ngr_confirmed
 * @property int $site_user_group_id
 * @property string $token
 * @property \Cake\I18n\Time $token_expires
 * @property string $api_token
 * @property \Cake\I18n\Time $activation_date
 * @property \Cake\I18n\Time $tos_date
 * @property bool $active
 * @property bool $is_superuser
 * @property string $role
 * @property \Cake\I18n\Time $created
 * @property \Cake\I18n\Time $modified
 *
 * @property \App\Model\Entity\SiteUserGroup $site_user_group
 */
class SiteUser extends User
{

protected $_accessible = [
    '*' => true,
    'id' => false,
    'role' => false,
];

}

And here's the new Table file:

namespace App\Model\Table;

use CakeDC\Users\Model\Table\UsersTable;

class SiteUsersTable extends UsersTable
{

}

bootstrap.php contains the following lines:

Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);

And users.php has this:

return [
'Users.table' => 'SiteUsers',
];

I'm feeling like I'm just not understanding this, but it seems like it should be a common enough request that there'd be at least a decent example out there.

Can anyone tell me why the changes I made didn't have the effect I was expecting (that the site_users table would be updated by the plugin), or what I need to do to achieve this in another way?

Many Thanks

1

There are 1 answers

1
steinkel On

Note you just need to add a migration and create the fields to the users table if you just need to add a couple extra fields.

Add $this->table('site_users'); to your new Table initialize method and let CakePHP know you are going to use a table with another name (not 'users')

Looks like you are loading the plugin config override in the wrong order, try this in you bootstrap.php file

Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);

As it documented here https://github.com/CakeDC/users/blob/master/Docs/Documentation/Configuration.md

Or you can override this config after plugin load this way:

Configure::write('Users.table', 'SiteUsers');