dereuromark / cakephp-hashid

CakePHP plugin to use hashids for your database table lookups
MIT License
36 stars 13 forks source link

$table->exists fails, $table->get succeeds? #13

Closed chopstik closed 6 years ago

chopstik commented 7 years ago

Seeing a difference in the application of Hashid when using get() & exists() in a Controller. In the following:

debug($eventsTable->exists(['id' => $id])); returns false debug($eventsTable->get($id)); returns the entity object as expected.

The Hashid debug setting has no impact. Cake's exist() appears only to be a normal find method on a Table so presume it should be using the normal call backs Hashid is using? Any ideas why the exist() method would be returning false and not true as expected? Removing the Hashid behaviour from AppTable::initialize 'solves' the problem in that both methods return true/entity as expected.

dereuromark commented 7 years ago

I think the ->enableHydration(false) part is the problem... It prevents the execution of the part that is needed for it to work here. Not sure if there is a workaround other than adding a trait to the model overwriting the core one.

chopstik commented 7 years ago

Cheers Mark.

dereuromark commented 7 years ago

Are you able to provide a trait HashidTrait that could be added to the table in order to fix the issue? Let me know how things work out. If that even works etc.

chopstik commented 7 years ago

We'll take a look for sure.

dereuromark commented 7 years ago

Thanks a lot. Either way someone maybe can provide a small adjustment to the documentation about this method not working directly.

dereuromark commented 7 years ago

It turns out it is

->select(['existing' => 1])

which breaks it.

dereuromark commented 7 years ago

ping @chopstik

dereuromark commented 6 years ago

https://github.com/dereuromark/cakephp-hashid/pull/14 should solve it?