Closed EronarDiaras closed 2 years ago
If you don't have the file config/users.php create one with:
return [
'Users.table' => 'MyUsers',
];
You don't need the code Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
About the name, you should use the one you defined in your Accounts model associations
PS: Your custom model class can have any name that works for you
Is this database structure okay? Or use it: accounts_users -> accounts_my_users users -> my_users
The structure is okay.
So what could be wrong? What did I miss?
config/users.php:
<?php
return [
'Users.Social.login' => false,
'Users.reCaptcha.key' => '6Lc71YkeAAAAAA*********',
'Users.reCaptcha.secret' => '6Lc71YkeAA******',
'Users.reCaptcha.registration' => true,
'Users.reCaptcha.login' => true,
'Users.table' => 'MyUsers',
];
src/Application.php:
public function bootstrap(): void
{
parent::bootstrap();
if (PHP_SAPI === 'cli') {
$this->bootstrapCli();
} else {
FactoryLocator::add(
'Table',
(new TableLocator())->allowFallbackClass(false)
);
}
if (Configure::read('debug')) {
$this->addPlugin('DebugKit');
}
$this->addPlugin(\CakeDC\Users\Plugin::class);
Configure::write('Users.config', ['users']);
$this->getEventManager()->on(new \App\Event\UsersListener());
}
AccountsController.php:
public function index()
{
// $accounts = $this->paginate($this->Accounts);
$query = $this->Accounts->find('all')->contain(['Users'])->all();
foreach ($query as $accounts) {
echo $accounts->users[0]->text;
}
//$this->set(compact('accounts'));
}
AccountsTable.php:
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('accounts');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
//$this->belongsToMany('MyUsers');
$this->belongsToMany('Users');
}
MyUsersTable.php:
public function initialize(array $config): void
{
parent::initialize($config);
$this->belongsToMany('Accounts');
}
You should use the same name in the belongsToMany and in the contain list, also you may need to define the className for the belongsToMany association.
You have two options.
This:
$this->belongsToMany('MyUsers');
$query = $this->Accounts->find('all')->contain(['MyUsers'])->all();
or
$this->belongsToMany('Users', [
'className' => 'MyUsers',
]);
$query = $this->Accounts->find('all')->contain(['Users'])->all();
``
Are you getting any error message?
Yes, it works! Many thanks!
$this->belongsToMany('Users', [
'className' => 'MyUsers',
]);
Can anyone give a complete example of extending model? Together with database structure?
I followed the instructions from docs: https://github.com/CakeDC/users/blob/master/Docs/Documentation/Extending-the-Plugin.md
CakePHP version: 4.3.5 CakeDC version: 11.0.0
but, for example, it returns an error:
When do I call, what name should I do? Users or MyUsers?
Can anyone give a complete example of extending model? Together with database structure?