Add polymorphic join helpers (joinMorph, leftJoinMorph, rightJoinMorph)
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -108,6 +108,35 @@ File_Attachment_Model::where('fileable_type', 42)->get();
|
||||
|
||||
Supported methods: `where()`, `orWhere()`, `whereIn()`, `orWhereIn()`, `whereNotIn()`, `orWhereNotIn()`
|
||||
|
||||
## Polymorphic Join Helpers
|
||||
|
||||
Join tables with polymorphic columns using dedicated helpers:
|
||||
|
||||
```php
|
||||
// Get contacts that have attachments (INNER JOIN)
|
||||
Contact_Model::query()
|
||||
->joinMorph('file_attachments', 'fileable')
|
||||
->select('contacts.*', 'file_attachments.filename')
|
||||
->get();
|
||||
|
||||
// Get all contacts, with attachments if they exist (LEFT JOIN)
|
||||
Contact_Model::query()
|
||||
->leftJoinMorph('file_attachments', 'fileable')
|
||||
->get();
|
||||
|
||||
// Explicit class (when querying from a different model)
|
||||
SomeModel::query()
|
||||
->leftJoinMorph('file_attachments', 'fileable', Contact_Model::class)
|
||||
->get();
|
||||
```
|
||||
|
||||
Available methods: `joinMorph()`, `leftJoinMorph()`, `rightJoinMorph()`
|
||||
|
||||
Parameters:
|
||||
- `$table` - Table with polymorphic columns (e.g., `'file_attachments'`)
|
||||
- `$morphName` - Column prefix (e.g., `'fileable'` for `fileable_type`/`fileable_id`)
|
||||
- `$morphClass` - Optional class name (defaults to current model)
|
||||
|
||||
## Important Notes
|
||||
|
||||
- **Simple Names Only**: Always use simple class names (`Contact_Model`), never FQCNs
|
||||
|
||||
@@ -130,6 +130,66 @@ LARAVEL MORPH MAP INTEGRATION
|
||||
The morph map uses simple class names (e.g., "Contact_Model") not fully
|
||||
qualified names, matching how RSX models work throughout the framework.
|
||||
|
||||
QUERY BUILDER INTEGRATION
|
||||
Transparent WHERE Clauses
|
||||
|
||||
The query builder automatically converts type_ref columns in WHERE
|
||||
clauses. You can use class name strings directly:
|
||||
|
||||
// All of these work - class names auto-converted to IDs
|
||||
File_Attachment_Model::where('fileable_type', 'Contact_Model')->get();
|
||||
File_Attachment_Model::where('fileable_type', '=', 'Contact_Model')->get();
|
||||
File_Attachment_Model::where(['fileable_type' => 'Contact_Model'])->get();
|
||||
|
||||
// whereIn also works
|
||||
File_Attachment_Model::whereIn('fileable_type', [
|
||||
'Contact_Model',
|
||||
'Project_Model'
|
||||
])->get();
|
||||
|
||||
// Integer IDs still work (pass-through)
|
||||
File_Attachment_Model::where('fileable_type', 42)->get();
|
||||
|
||||
Supported methods: where(), orWhere(), whereIn(), orWhereIn(),
|
||||
whereNotIn(), orWhereNotIn()
|
||||
|
||||
Polymorphic Join Helpers
|
||||
|
||||
Join tables with polymorphic columns using dedicated helpers:
|
||||
|
||||
// Get contacts that have attachments (INNER JOIN)
|
||||
Contact_Model::query()
|
||||
->joinMorph('file_attachments', 'fileable')
|
||||
->select('contacts.*', 'file_attachments.filename')
|
||||
->get();
|
||||
|
||||
// Get all contacts, with attachments if they exist (LEFT JOIN)
|
||||
Contact_Model::query()
|
||||
->leftJoinMorph('file_attachments', 'fileable')
|
||||
->get();
|
||||
|
||||
// RIGHT JOIN
|
||||
Contact_Model::query()
|
||||
->rightJoinMorph('file_attachments', 'fileable')
|
||||
->get();
|
||||
|
||||
Parameters:
|
||||
$table - Table with polymorphic columns (e.g., 'file_attachments')
|
||||
$morphName - Column prefix (e.g., 'fileable' for fileable_type/fileable_id)
|
||||
$morphClass - Optional class name (defaults to current model)
|
||||
|
||||
Explicit class (when querying from a different model context):
|
||||
|
||||
SomeModel::query()
|
||||
->leftJoinMorph('file_attachments', 'fileable', Contact_Model::class)
|
||||
->get();
|
||||
|
||||
The generated SQL is equivalent to:
|
||||
|
||||
LEFT JOIN file_attachments
|
||||
ON file_attachments.fileable_id = contacts.id
|
||||
AND file_attachments.fileable_type = <type_ref_id>
|
||||
|
||||
FORM HANDLING
|
||||
Client-Side Format
|
||||
|
||||
|
||||
Reference in New Issue
Block a user