mirror of
https://github.com/ent/ent.git
synced 2026-03-05 19:35:23 +03:00
doc/features: add dynamic values support (#3436)
This commit is contained in:
@@ -144,6 +144,30 @@ The above code will produce the following SQL query:
|
||||
SELECT SUM(LENGTH(name)) FROM `pet`
|
||||
```
|
||||
|
||||
#### Select and Scan Dynamic Values
|
||||
|
||||
If you work with SQL modifiers and need to scan dynamic values not present in your Ent schema definition, such as
|
||||
aggregation or custom ordering, you can apply `AppendSelect`/`AppendSelectAs` to the `sql.Selector`. You can later
|
||||
access their values using the `Value` method defined on each entity:
|
||||
|
||||
```go {6,11}
|
||||
const as = "name_length"
|
||||
|
||||
// Query the entity with the dynamic value.
|
||||
p := client.Pet.Query().
|
||||
Modify(func(s *sql.Selector) {
|
||||
s.AppendSelectAs("LENGTH(name)", as)
|
||||
}).
|
||||
FirstX(ctx)
|
||||
|
||||
// Read the value from the entity.
|
||||
n, err := p.Value(as)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Name length: %d == %d", n, len(p.Name))
|
||||
```
|
||||
|
||||
#### Modify Example 2
|
||||
|
||||
```go
|
||||
|
||||
Reference in New Issue
Block a user