Properties added in bake do not match those used internally (CakePHP4)

Asked 2 years ago, Updated 2 years ago, 57 views

Regarding the question I posted in the English version (below), I may not have explained it well.
If anyone understands, please let me know.
https://stackoverflow.com/posts/comments/117799800?noredirect=1
 
 
In the migration, we created a table called foo_logs and baked the model and controller under the name FooLogs.The controller has the property $FooLogs added (in the DOC).

The following code generated by bake successfully retrieved data:
 $fooLogs=$this->paginate($this->FooLogs);
However, if you make the following changes, the exception is "Call to a member function find() on null".
 $fooLogs=$this->paginate($this->FooLogs->find('all')->where($Conditions)));

The debugger tried to identify the model class in Foologs instead of FooLogs (it does not match as a string).Therefore, the model class cannot be identified, and $this->FooLogs will eventually remain null, resulting in an exception.
If the code is generated by bake, it seems to be identified by "FooLogs".

Is there any reason why "Foologs" is used to identify model classes when using find()?Does the word log have a special meaning in CakePHP?

Thank you for your cooperation.

cakephp

2022-09-30 17:10

2 Answers

*It has been resolved, so I will replace the answer.

The reason is the link output to the page of the controller, as shown below.It was because the controller name was "foologs".When resolving model classes, only initials appear to be capitalized (foologs → Foologs).
$this->Html->link('○○○○', ['controller'=>'foologs')

It's called a mediocre mistake.Verified that the controller name was written correctly and that it worked fine.

Sorry for the trouble.


2022-09-30 17:10

First of all, I will write down how to deal with it.

If you change $this->FooLogs to $this->Foologs, it will work, but basically you should use the name (FooLogs) you specified during bake.
Now load the model with initialize() on the controller.This allows access from within each method without changing the name specified during bake.
 
 
*The purpose of this post is to know why find() sometimes uses different names to identify model classes than when baking, so we will continue to wait for your feedback.


2022-09-30 17:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.