cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Model findFirst() and UUID

Open zak-wojtek77 opened this issue 6 years ago • 5 comments

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

zak-wojtek77 avatar Jan 17 '20 19:01 zak-wojtek77

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?

niden avatar Jan 17 '20 20:01 niden

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'"

zak-wojtek77 avatar Jan 18 '20 06:01 zak-wojtek77

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)

renatogabrielbr avatar Jan 21 '20 06:01 renatogabrielbr

For now, you can write like that and it will work:

$uuid = '5741bfd7-6870-40b7-adf6-cbacb515b9a9';
$model = ModelWithStringPrimary::findFirst([
    'uuid = ?0',
    'bind' => [$uuid],
]);

Jeckerson avatar Jan 22 '20 15:01 Jeckerson

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.

Jeckerson avatar Jan 27 '20 17:01 Jeckerson