Difference between #8 and #9 of
UUID instead of an auto-increment integer for ID with Active Record

Revision #9 has been created by grigori on Nov 25, 2019, 10:50:00 PM with the memo:

fixing the markup
« previous (#8) next (#10) »

Changes

Title unchanged

UUID instead of an auto-increment integer for ID with Active Record

Category unchanged

How-tos

Yii version unchanged

2.0

Tags unchanged

mysql,active record,REST,UUID

Content changed

[...]
Adding `getId()`/`setId()` won't help - data comes from a client in JSON and fills the model object with a `setAttributes()` call avoiding generic magic methods.

Here's the hack:

Step 1. Add a
`private $idText;` property ```php use yii\db\ActiveRecord;
 
class Video extends ActiveRecord
{
private $idText;
[...]
From the other hand, the first valiator calls `$model->id` triggering the getter before the UUID is saved to the private property so I need to serve the value from user input.

It is strange to mutate data in a validator, but I found this is the only solution. I belive I shouldn't use
`beforeSave()` callback to set the binary value for SQL, and return the text value back in `afterSave()`, supporting this code later will be a hell. So, now you can go the generic mysqlMySQL way

Step 4. add a virtual column

```sql
ALTER TABLE t1 ADD id_text varchar(36) generated always as
[...]
5 0
4 followers
Viewed: 56 709 times
Version: 2.0
Category: How-tos
Written by: grigori
Last updated by: samdark
Created on: Nov 25, 2019
Last updated: 4 years ago
Update Article

Revisions

View all history