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

fetchTable() does not properly resolve when used via LocatorAwareTrait in a service class #12

Closed
LordSimal opened this issue Mar 21, 2023 · 8 comments

Comments

@LordSimal
Copy link
Contributor

LordSimal commented Mar 21, 2023

<?php
declare(strict_types=1);

namespace App\Service;

use Cake\ORM\Locator\LocatorAwareTrait;

class TestService {
    use LocatorAwareTrait;

    public function test(): void
    {
        $table = $this->fetchTable('MyModel');
        $entity = $table->newEmptyEntity();
        $name = $entity->name;
    }

}

does give the following error

 ------ ------------------------------------------------------------------------- 
  Line   src/Service/TestService.php                                              
 ------ ------------------------------------------------------------------------- 
  15     Access to an undefined property Cake\Datasource\EntityInterface::$name.  
 ------ ------------------------------------------------------------------------- 

but the same 3 lines inside e.g. a controller action work fine.

@LordSimal
Copy link
Contributor Author

Just looked through the other issues and this is related to phpstan not supporting trait methods via DynamicReturnTypeExtension
phpstan/phpstan#5761

@rochamarcelo
Copy link
Contributor

Yes, trait is not supported. If you have a base Service class that uses LocatorAwareTrait you could have phpstan config as it is done at https://github.com/CakeDC/cakephp-phpstan/blob/master/extension.neon#L13
. Note that $this->getTableLocator()->get('MyModel') should not cause error with phpstan.

@LordSimal
Copy link
Contributor Author

LordSimal commented Mar 23, 2023

yea, i have always defaulted to $this->getTableLocator()->get('MyModel') till now 😁
But it was a question recently in our slack support chat so I just opened this issue so it is documented.

@dereuromark
Copy link
Contributor

Is there a solution for traits and fetchTable() yet?

@rochamarcelo
Copy link
Contributor

I could not find a good solution for this issue.

@dereuromark
Copy link
Contributor

Interesting:
For me

$this->fetchTable('DatabaseLog.DatabaseLogs')->removeDuplicates();

works, but

protected ?string $defaultTable = 'DatabaseLog.DatabaseLogs';

and

$this->fetchTable()->removeDuplicates();

results in

34     Call to an undefined method Cake\ORM\Table::removeDuplicates().

Probably the defaultTable property should be read for empty argument?

@rochamarcelo
Copy link
Contributor

Yes, sent #19

@steinkel
Copy link
Member

Closed per #19, please reopen if needed. Thanks,

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

No branches or pull requests

4 participants