You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/9.0/reader/record-type-inference.md
+30-23Lines changed: 30 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@ description: Detect your CSV field type using an ergonomic and deterministic typ
6
6
7
7
# Schema inference
8
8
9
+
<pclass="message-notice">Added in version <code>9.29.0</code>.</p>
10
+
9
11
CSV files do not contain type information: every value is initially read as text. Schema inference allows you to inspect a CSV and automatically determine the most appropriate type for each column.
10
12
11
13
The inferred schema can then be used to parse the records into their corresponding PHP values.
@@ -20,10 +22,10 @@ id,name,active,amount
20
22
21
23
schema inference can determine that:
22
24
23
-
-`id` is numeric;
24
-
-`name` is a string;
25
-
-`active` is boolean;
26
-
-`amount` is numeric.
25
+
-`id` is numeric;
26
+
-`name` is a string;
27
+
-`active` is boolean;
28
+
-`amount` is numeric.
27
29
28
30
The feature is available through `Reader::inferSchema()` and `Reader::inferRecords()`.
29
31
@@ -137,7 +139,7 @@ For example, with a sample limit of `100`, the first 100 records are used to det
137
139
138
140
The default sample size is `10`.
139
141
140
-
## Controlling the inferred types
142
+
###Controlling the inferred types
141
143
142
144
The `Inspector` also controls which field types are considered during inference.
143
145
@@ -168,7 +170,7 @@ The available built-in field types cover common values such as booleans, numbers
168
170
169
171
Only the fields included in the `FieldList` are considered during schema inference.
170
172
171
-
## Built-in field types
173
+
####Built-in field types
172
174
173
175
The inspection engine provides a field implementation for each supported field type:
This distinction is important: **a field being available does not mean that it is automatically used during inference**. Only fields present in the `FieldList` are considered by the `Inspector`.
212
214
213
-
## Custom field types
215
+
####Custom field types
214
216
215
217
If the built-in field types do not cover a particular kind of value, a custom field can be added to the inspector.
This value is useful for illustrating the difference between the three APIs.
310
312
311
-
### `getRecords()`: read the CSV as-is
313
+
### Read the CSV as-is
312
314
313
315
`getRecords()` does not interpret the values.
314
316
@@ -332,7 +334,7 @@ No validation or type conversion takes place.
332
334
333
335
Use `getRecords()` when you want to handle the CSV values yourself.
334
336
335
-
### `inferRecords()`: infer the types
337
+
### Infer the types
336
338
337
339
`inferRecords()` examines the CSV and builds a schema from the values it finds.
338
340
@@ -356,7 +358,9 @@ The `gender` value remains a string because the default field list does not know
356
358
357
359
This illustrates an important property of inference:
358
360
359
-
> `inferRecords()` only applies the types that can be inferred from the configured field list.
361
+
<pclass="message-notice">
362
+
<code>inferRecords()</code> only applies the types that can be inferred from the configured field list.
363
+
</p>
360
364
361
365
#### Inferring the schema
362
366
@@ -431,7 +435,7 @@ In short:
431
435
-**`inferSchema()`** tells you *what the CSV is understood to contain*.
432
436
-**`inferRecords()`** gives you the records *according to that understanding*.
433
437
434
-
### `inferRecords()` with a custom inspector
438
+
### Using a custom inspector
435
439
436
440
The inference can be configured when the CSV contains domain-specific types.
437
441
@@ -475,7 +479,7 @@ Because `"R"` is not a valid case of `Gender`, the `EnumField` cannot parse it a
475
479
476
480
This is an important difference from `getRecordsAsObject()`: **inferred parsing is tolerant of values that cannot be parsed by the inferred field**.
477
481
478
-
### `getRecordsAsObject()`: enforce a known structure
482
+
### Enforce a known structure
479
483
480
484
When the target object is known, `getRecordsAsObject()` can explicitly define how each column should be converted.
481
485
@@ -532,14 +536,17 @@ The difference is not simply about type conversion. Each method has a different
532
536
|`inferRecords()`| Infer and parse data | Discovered from CSV | Parsed value or `null`|
533
537
|`getRecordsAsObject()`| Map to a known object | Explicitly defined | Exception |
534
538
535
-
#### Which one should I use?
536
-
537
-
Use **`getRecords()`** when you want the raw CSV values and will handle conversion yourself.
538
-
539
-
Use **`inferRecords()`** when you want the library to discover useful types from the CSV without having to define a complete data model.
540
539
541
-
Use **`getRecordsAsObject()`** when you have a known object model and want the CSV to be converted according to that model, with conversion failures treated as errors.
540
+
<divclass="message-notice">
541
+
<ul>
542
+
<li>Use <code>getRecords()</code> when you want the raw CSV values and will handle conversion yourself.</li>
543
+
<li>Use <code>inferRecords()</code> when you want the library to discover useful types from the CSV without having to define a complete data model.</li>
544
+
<li>Use <code>getRecordsAsObject()</code> when you have a known object model and want the CSV to be converted according to that model, with conversion failures treated as errors.</li>
545
+
</ul>
546
+
</div>
542
547
543
548
In summary:
544
549
545
-
> **`getRecords()` preserves the input, `inferRecords()` interprets the input, and `getRecordsAsObject()` validates the input against an explicit object model.**
550
+
<pclass="message-warning">
551
+
<code>getRecords()</code> preserves the input, <code>inferRecords()</code> interprets the input, and <code>getRecordsAsObject()</code> validates the input against an explicit object model.
0 commit comments