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

Revision #12 has been created by grigori on Nov 25, 2019, 11:10:29 PM with the memo:

added the fields() callback
« previous (#11) next (#13) »

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

[...]
Active Record does not call the getter method if attributes contain the property. It should not be this way, so I return the default component behavior and make ID returned the right way.
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 way. I belive I shouldn't use `beforeSave()` callback to set the binary value for generating SQL, and return the text value back in `afterSave()` - supporting this code would be a classic hell like `#define true false;`.

Step 4. Define the mapping for output
 
 
```php
 
public function fields()
 
{
 
    $fields = parent::fields();
 
    $fields['id'] =function(){return $this->getId();};
 
```
 
 
This method is used by RESTful serializers to format data when you access your API with `GET /video` requests.
 
 
So, now you can go the generic MySQL way Step 45. add a virtual column

```sql
ALTER TABLE t1 ADD id_text varchar(36) generated always as
(insert(
insert(
[...]
5 0
4 followers
Viewed: 57 185 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