Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: You must specify either the alias or the table option for the constructor #6

Closed
asgraf opened this issue Jan 11, 2023 · 4 comments · Fixed by #7
Closed

Error: You must specify either the alias or the table option for the constructor #6

asgraf opened this issue Jan 11, 2023 · 4 comments · Fixed by #7

Comments

@asgraf
Copy link
Contributor

asgraf commented Jan 11, 2023

All of my app tables are extending custom \App\Model\Table\Table
Your plugin incorrectly tries to create instance of this \App\Model\Table\Table class:

TableRegistry.php:95, Cake\ORM\TableRegistry::get('App.')
Gate.php:157, AssociationsDebugger\Gate->_associations('', 'App')
Gate.php:136, AssociationsDebugger\Gate->getAssociations()
Gate.php:97, AssociationsDebugger\Gate->associations([])
AssociationsController.php:52, AssociationsDebugger\Controller\AssociationsController->index()

And ends up with following error page:

[Cake\Core\Exception\CakeException] You must specify either the `alias` or the `table` option for the constructor. in /application/vendor/cakephp/cakephp/src/ORM/Table.php on line 427

Your plugin has same problem with dereuromark/cakephp-tools plugin because it contains \Tools\Model\Table\Table class

@zunnu
Copy link
Owner

zunnu commented Jan 23, 2023

What version are you running? Please try to update to the newest version.

This plugin uses Cakes TableRegistry get to initiate tables earlier this method was using deprecated version (TableRegistry::get) but has now been updated to newest way (TableRegistry::getTableLocator()->get)

I did some fast testing on this.

I created a custom Table.php that looks like this

<?php
declare(strict_types=1);

namespace App\Model\Table;

use Cake\ORM\Table as BaseTable;

class Table extends BaseTable
{
    public function MyCustomFunction(): string
    {
        return 'im custom';
    }
}

And this is my UsersTable that extends the custom table

<?php
declare(strict_types=1);

namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use App\Model\Table\Table;
use Cake\Validation\Validator;

/**
 * Users Model
 *
 * @property \App\Model\Table\LoginTokensTable&\Cake\ORM\Association\HasMany $LoginTokens
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class UsersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('LoginTokens', [
            'foreignKey' => 'user_id',
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator): Validator
    {
        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules): RulesChecker
    {
        return $rules;
    }
}

This setup worked for me without issues.

@asgraf
Copy link
Contributor Author

asgraf commented Jan 24, 2023

I just tested on latest 2.0.1 version of your plugin on CakePHP 4.4.10 and PHP 8.1.12 and problem still persists.
Try visiting https://localhost/associations-debugger (replace localhost with your domain) with one of one of following examples:

  1. add any behavior that calls $table->getAlias() or $table->getTable() internally (my case)
class Table extends \Cake\ORM\Table
{
    public function initialize(array $config): void
    {
        $this->addBehavior('Search.Search');
    }
}
  1. make Table.php abstract
abstract class Table extends \Cake\ORM\Table
{
    //something
}
  1. call $this->getAlias() or $this->getTable() direcly
class Table extends \Cake\ORM\Table
{
    public function initialize(array $config): void
    {
        debug($this->getTable());
    }
}

Contents of Table.php does not really matters. What matters is that you create instance of this class in first place (and you do it without passing required parameters to table constructor)
I have already provided you a pull request with fix for this issue: #7

@asgraf
Copy link
Contributor Author

asgraf commented Jan 24, 2023

When creating instance of SomethingTable, passing alias or table name is not required because Something alias and something table name can be inferred from SomethingTable class name.
This is not the case when creating direct instance of Table class.
In that case you must provide name/alias as CakeExcepion message states:

You must specify either the `alias` or the `table` option for the constructor.

@zunnu zunnu closed this as completed in #7 Jan 24, 2023
@zunnu
Copy link
Owner

zunnu commented Jan 24, 2023

Thank you for the clearer example. I have reproduced this problem and the changes in the pull request were correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants