[BUG]: Model findFirst() and UUID
Describe the bug In documentation is written:
// Find record with id = 3
$robot = Robots::findFirst(3);
it works but only if ID is type of Integer, if ID is UUID that does not work.
Details
- Phalcon version: 4.0.0 (3.4.1 also)
- PHP Version: 7.2
- Operating System: Debian
- Installation type: Compiling from source in Docker
- Server: Nginx
- Other related info (Database, table schema): MySQL 5.5
Can you explain a bit what you mean by if ID is UUID ? Are you talking about the type of the column in MySQL or are you using a string/varchar to store the UUID?
Sometimes primary key may be char(36) for example "1c53079c-249e-0c63-af8d-52413bfa2a2b". This problem is because in Phalcon in this function: Robots::findFirst("1c53079c-249e-0c63-af8d-52413bfa2a2b"); "1c53079c-249e-0c63-af8d-52413bfa2a2b" is treat like "integer", in generated SQL is like that: "select * from robot where id = 1c53079c-249e-0c63-af8d-52413bfa2a2b" and correct should be: "select * from robot where id = '1c53079c-249e-0c63-af8d-52413bfa2a2b'"
Can confirm on phalcon v4.0.2 + Php 7.4.1 Postgresql 12.1
Table Primary key uuid type, does not have a sequential ID column type uuid
$test = VisionPosition::findFirst("5741bfd7-6870-40b7-adf6-cbacb515b9a9");
var_dump($test->toArray());
The method broke on dash ( - ) and look's like transform on a column instead of searching on a primary_key, Column '5741bfd7' doesn't belong to any of the selected models (1)
For now, you can write like that and it will work:
$uuid = '5741bfd7-6870-40b7-adf6-cbacb515b9a9';
$model = ModelWithStringPrimary::findFirst([
'uuid = ?0',
'bind' => [$uuid],
]);
After internal team discussions with implementation of autodetect string primary key, and also after benchmarks. We opt to leave it as is, because by design it wasn't never supposed to support that approach.
Use bind approach as mentioned above.
Docs updated and described this situation - https://docs.phalcon.io/4.0/en/db-models#findfirst
Closed.