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
|
5. Return model object, custom array, or false
|
||||||
|
|
||||||
Return Value Options:
|
Return Value Options:
|
||||||
- Model object: Serialized via to_export_array(), includes __MODEL for hydration
|
- Model object: Serialized via toArray(), includes __MODEL for hydration
|
||||||
- Custom array: Full control over data shape, include computed/formatted fields
|
- Augmented array: Start with $model->toArray(), add computed fields (recommended)
|
||||||
- false: Record not found or unauthorized (MUST be false, not null)
|
- 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:
|
Basic Implementation:
|
||||||
use Ajax_Endpoint_Model_Fetch;
|
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
|
class Client_Model extends Rsx_Model
|
||||||
{
|
{
|
||||||
#[Ajax_Endpoint_Model_Fetch]
|
#[Ajax_Endpoint_Model_Fetch]
|
||||||
@@ -147,31 +151,36 @@ IMPLEMENTING FETCHABLE MODELS
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return formatted array with computed fields
|
// Start with model's toArray() to get __MODEL and base data
|
||||||
return [
|
$data = $client->toArray();
|
||||||
'id' => $client->id,
|
|
||||||
'name' => $client->name,
|
// Augment with computed/formatted fields
|
||||||
'status' => $client->status,
|
$data['status_label'] = ucfirst($client->status);
|
||||||
// Computed display fields
|
$data['status_badge'] = match($client->status) {
|
||||||
'status_label' => ucfirst($client->status),
|
'active' => 'bg-success',
|
||||||
'status_badge' => match($client->status) {
|
'inactive' => 'bg-secondary',
|
||||||
'active' => 'bg-success',
|
default => 'bg-warning'
|
||||||
'inactive' => 'bg-secondary',
|
};
|
||||||
default => 'bg-warning'
|
$data['created_at_formatted'] = $client->created_at->format('M d, Y');
|
||||||
},
|
$data['created_at_human'] = $client->created_at->diffForHumans();
|
||||||
// Formatted dates
|
$data['region_name'] = $client->region_name();
|
||||||
'created_at_formatted' => $client->created_at->format('M d, Y'),
|
$data['country_name'] = $client->country_name();
|
||||||
'created_at_human' => $client->created_at->diffForHumans(),
|
|
||||||
// Related data
|
return $data;
|
||||||
'region_name' => $client->region_name(),
|
|
||||||
'country_name' => $client->country_name(),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Note: When returning an array, the JavaScript side receives the plain
|
IMPORTANT: Always start with $model->toArray() when augmenting data.
|
||||||
object (not a hydrated model instance). This is intentional - it gives
|
This preserves the __MODEL property needed for JavaScript hydration.
|
||||||
full control over the data shape for view/edit pages.
|
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
|
JAVASCRIPT USAGE
|
||||||
Single Record Fetching:
|
Single Record Fetching:
|
||||||
|
|||||||
Reference in New Issue
Block a user