Add get_polymorphic_parent() helper for type_ref_columns compatibility
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -425,6 +425,40 @@ abstract class Rsx_Model_Abstract extends Model
|
||||
*/
|
||||
protected static $type_ref_columns = [];
|
||||
|
||||
/**
|
||||
* Get a polymorphic parent using type_ref_columns
|
||||
*
|
||||
* Laravel's morphTo() reads raw attributes, bypassing accessors.
|
||||
* Since type_ref_columns stores integers but accessors return class names,
|
||||
* morphTo() crashes. Use this helper instead.
|
||||
*
|
||||
* Usage in model:
|
||||
* public function getParentAttribute() {
|
||||
* return $this->get_polymorphic_parent('parent_type', 'parent_id');
|
||||
* }
|
||||
*
|
||||
* @param string $type_column Column storing the type (e.g., 'parent_type')
|
||||
* @param string $id_column Column storing the ID (e.g., 'parent_id')
|
||||
* @return \Illuminate\Database\Eloquent\Model|null
|
||||
*/
|
||||
protected function get_polymorphic_parent(string $type_column, string $id_column)
|
||||
{
|
||||
$type = $this->$type_column; // Uses accessor - gets class name
|
||||
$id = $this->$id_column;
|
||||
|
||||
if (!$type || !$id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Resolve through morph map (e.g., 'Task_Model' → 'Rsx\Models\Task_Model')
|
||||
$class = \Illuminate\Database\Eloquent\Relations\Relation::getMorphedModel($type);
|
||||
if (!$class) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $class::find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the casts array with automatic type casting based on database schema
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user