Update model_fetch man page with correct augmented array pattern
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -64,10 +64,14 @@ IMPLEMENTING FETCHABLE MODELS
|
||||
5. Return model object, custom array, or false
|
||||
|
||||
Return Value Options:
|
||||
- Model object: Serialized via to_export_array(), includes __MODEL for hydration
|
||||
- Custom array: Full control over data shape, include computed/formatted fields
|
||||
- Model object: Serialized via toArray(), includes __MODEL for hydration
|
||||
- Augmented array: Start with $model->toArray(), add computed fields (recommended)
|
||||
- false: Record not found or unauthorized (MUST be false, not null)
|
||||
|
||||
Note: Always use toArray() as the base when returning arrays. This preserves
|
||||
the __MODEL property required for JavaScript hydration. See "Augmented Array
|
||||
Return" section below for the correct pattern.
|
||||
|
||||
Basic Implementation:
|
||||
use Ajax_Endpoint_Model_Fetch;
|
||||
|
||||
@@ -136,7 +140,7 @@ IMPLEMENTING FETCHABLE MODELS
|
||||
}
|
||||
}
|
||||
|
||||
Custom Array Return (recommended for CRUD pages):
|
||||
Augmented Array Return (recommended for CRUD pages):
|
||||
class Client_Model extends Rsx_Model
|
||||
{
|
||||
#[Ajax_Endpoint_Model_Fetch]
|
||||
@@ -147,31 +151,36 @@ IMPLEMENTING FETCHABLE MODELS
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return formatted array with computed fields
|
||||
return [
|
||||
'id' => $client->id,
|
||||
'name' => $client->name,
|
||||
'status' => $client->status,
|
||||
// Computed display fields
|
||||
'status_label' => ucfirst($client->status),
|
||||
'status_badge' => match($client->status) {
|
||||
// Start with model's toArray() to get __MODEL and base data
|
||||
$data = $client->toArray();
|
||||
|
||||
// Augment with computed/formatted fields
|
||||
$data['status_label'] = ucfirst($client->status);
|
||||
$data['status_badge'] = match($client->status) {
|
||||
'active' => 'bg-success',
|
||||
'inactive' => 'bg-secondary',
|
||||
default => 'bg-warning'
|
||||
},
|
||||
// Formatted dates
|
||||
'created_at_formatted' => $client->created_at->format('M d, Y'),
|
||||
'created_at_human' => $client->created_at->diffForHumans(),
|
||||
// Related data
|
||||
'region_name' => $client->region_name(),
|
||||
'country_name' => $client->country_name(),
|
||||
];
|
||||
};
|
||||
$data['created_at_formatted'] = $client->created_at->format('M d, Y');
|
||||
$data['created_at_human'] = $client->created_at->diffForHumans();
|
||||
$data['region_name'] = $client->region_name();
|
||||
$data['country_name'] = $client->country_name();
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
Note: When returning an array, the JavaScript side receives the plain
|
||||
object (not a hydrated model instance). This is intentional - it gives
|
||||
full control over the data shape for view/edit pages.
|
||||
IMPORTANT: Always start with $model->toArray() when augmenting data.
|
||||
This preserves the __MODEL property needed for JavaScript hydration.
|
||||
The result is a hydrated model instance with your extra fields added.
|
||||
|
||||
DO NOT manually construct the return array like this (outdated pattern):
|
||||
return [
|
||||
'id' => $client->id,
|
||||
'name' => $client->name,
|
||||
// ... manually listing fields
|
||||
];
|
||||
This loses __MODEL and returns a plain object instead of a model instance.
|
||||
|
||||
JAVASCRIPT USAGE
|
||||
Single Record Fetching:
|
||||
|
||||
Reference in New Issue
Block a user