Sunday, March 13, 2022

[FIXED] using FactoryLocator didnt work as class not found with cakephp4.2

Issue

in accessing a model withing a model in cakephp 4.2 the docs command FactoryLocator hasnt worked.

Class 'App\Model\Table\FactoryLocator' not found

use Cake\ORM\TableLocator;  //tried this
use Cake\Datasource\Locator\LocatorInterface; //tried this

class LessonsTable extends Table
 public function getTest1(){
   $clients = FactoryLocator::get('Table')->get('Clients'); //error here 



https://book.cakephp.org/4/en/orm/table-objects.html#using-the-tablelocator
https://api.cakephp.org/4.1/class-Cake.Datasource.FactoryLocator.html

Solution

Again ;)

use Cake\Datasource\FactoryLocator; // <--------------- use this

class LessonsTable extends Table
 public function getTest1(){
   $clients = FactoryLocator::get('Table')->get('Clients');

I don't know in which editor you write php code, but I would recommend that you use plugins that will suggest which php / cakephp methods are available in the current php file.

For example in https://code.visualstudio.com/ add some extesnions like

https://marketplace.visualstudio.com/search?term=php&target=VSCode&category=All%20categories&sortBy=Relevance

for code completion use https://marketplace.visualstudio.com/items?itemName=bmewburn.vscode-intelephense-client

Then when you type FactoryLo editor find class / method and suggest to you, it also automatically adds namespace if it is missing.

enter image description here



Answered By - Salines

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.