CakeDC / users

Users Plugin for CakePHP
https://www.cakedc.com
Other
521 stars 296 forks source link

Extending model not working #982

Closed EronarDiaras closed 2 years ago

EronarDiaras commented 2 years ago

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:

// config/bootstrap.php
Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
// Error: Class "Plugin" not found

When do I call, what name should I do? Users or MyUsers?

$query = $this->Accounts->find('all')->contain(['Users'])->all();
        foreach ($query as $accounts) {
            echo $accounts->users[0]->text;
        }
//in MyUsersTable.php: $this->belongsToMany('Accounts');
//in AccountsTable.php: $this->belongsToMany('Users'); //or MyUsers???
//and accounts_users databasetable or accounts_my_users???

Can anyone give a complete example of extending model? Together with database structure?

rochamarcelo commented 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

EronarDiaras commented 2 years ago

Is this database structure okay? Or use it: accounts_users -> accounts_my_users users -> my_users

cakedc-sql

rochamarcelo commented 2 years ago

The structure is okay.

EronarDiaras commented 2 years ago

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');
    }
rochamarcelo commented 2 years ago

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();
``
rochamarcelo commented 2 years ago

Are you getting any error message?

EronarDiaras commented 2 years ago

Yes, it works! Many thanks!

$this->belongsToMany('Users', [
    'className' => 'MyUsers',
]);