[Bug suspected] patchEntity() give wrong error code

Dear all,

I am trying to patch $userData which containing fail data (not unique username, and not in email format as expected). So the entity should has errors.

If I use if ($user[‘hasErrors’]) it always take me to opposite direction: my if is false when $user[‘hasErrors’] is true, and the other way round.

$user = $this->Users->patchEntity($user,$userData); //data array has errors
 if ($user['hasErrors']) {
     $this->Flash->success(__('The given user info is OK.'));
} else {
     $this->Flash->error(__('The user could not be saved. Please, try again.'));

If I use method as written in CakePHP 4 Book, debug said

Call to undefined method App\Model\Entity\User::errors()

Is there bug in this?

I am using CakePHP 4.0.3

To be clear, your code as written would appear to say that it’s OK if it has errors. Is that because you’ve changed it because of the issue that you think is happening, or is this just a simple logic error on your part?

Anyway, entity properties can often be accessed as if it’s an array, but I don’t know if that applies to hasErrors, or if you have to do it as $user->hasErrors().

In you conditional ($user[‘hasError’]) you’re banking on that array node containing a boolean value. Any other value and it would evaluate as truthy.

It will be much safer to use the method @Zuluru suggests since it will absolutely return a boolean.

I have to change my code logic in my code, so that it will work as expected.

Let’s talk about the framework point of view (not my code).
When data has validation error, patched Entity should have ‘hasErrors’ set to be true, right? But it does not.
And the method ->hasErrors() should returns boolean of true , right? But it does not, simply debugger kit complain that it is undefined method.

I am in doubt that I have wrong version of CakePHP, or maybe a beta version. DebugKit said I am using CakePHP 4.0.3, and I updated using composer.

I have tried that, but the DebugKit complains that it is undefined method. Strange. That is why I suspect it is a bug.

I have an installation of 4.0.3 and can access that method.

Possibly your User entity class does not extend Entity? The method in question is actually in EntityTrait I think. So possibly you could use that trait.

It does extend Entity

and it still say:

Call to undefined method App\Model\Entity\User::errors():clipboard:

The errors method does not exist in 4.x. It’s now hasErrors.

Thanks Zuluru, again,

I also has just track back to EntityInterface class that Entity implements, and also found no errors(), but hasErrors().

So, to conclude, the code of CakePHP does not have bug, but Book Documentation does, it would be great if someone could update it, newbie like me always blindly trust Documentation.

P/S: how could dreamingmind get that method in CakePHP 4.0.3 since 4.x Entity does not have that.

You said “the method ->hasErrors() should returns boolean of true , right? But it does not, simply debugger kit complain that it is undefined method”. @dreamingmind was responding to that in saying that it works for them.

Where in the book are you finding a reference to the errors method existing in 4.x? This page definitely does not mention it. Are you looking at the v3 version? It states that you should only use errors prior to 3.4.0.

1 Like

Dear Zuluru,

Let me clarify that again.

Firstly I have problem using errors() , Debug Kit always complain that it’s undefined. And we now agreed that the method does not exist in ver 4.x. I have just tried to find which part of Book refer to that method, but could not, I am sorry If I messed up with Book version.

Secondly, another problem i had, when I use hasErrors(), it return False when there IS error, and True when there IS NOT .

Well, Today I have wrote some code just to test the second problem above, and it turn out that I am wrong.

My apology for wasting your time. I had bad day yesterday , messing my mind with the code logic. I got confused.

1 Like